Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/core_ext.rb
Instance Method Summary collapse
- #append_crc16 ⇒ Object
- #append_sint(number, byte) ⇒ Object
- #append_uint(number, byte) ⇒ Object
- #check_crc16(remove_after_check = false) ⇒ Object
- #to_bytehex ⇒ Object
- #to_sint ⇒ Object
- #to_uint ⇒ Object
- #xor(array2) ⇒ Object
Instance Method Details
#append_crc16 ⇒ Object
36 37 38 |
# File 'lib/core_ext.rb', line 36 def append_crc16 append_uint(crc16, 2) end |
#append_sint(number, byte) ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/core_ext.rb', line 22 def append_sint(number, byte) raise 'Insufficient bytes' if number.abs >= (1 << (byte * 8)) sign = (number < 0) ? 1 : 0 number &= (1 << ((byte * 8) - 1)) - 1 self.append_uint(number, byte) self << (self.pop | (sign << 7)) end |
#append_uint(number, byte) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 |
# File 'lib/core_ext.rb', line 2 def append_uint(number, byte) raise 'Only support unsigned integer' if number < 0 raise 'Insufficient bytes' if number.abs >= (1 << (byte * 8)) until byte == 0 self << (number & 0xFF) number >>= 8 byte -= 1 end self end |
#check_crc16(remove_after_check = false) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/core_ext.rb', line 40 def check_crc16(remove_after_check = false) orig_crc = pop(2) old_crc = (orig_crc[1] << 8) + orig_crc[0] new_crc = crc16 concat(orig_crc) unless remove_after_check old_crc == new_crc end |
#to_bytehex ⇒ Object
52 53 54 |
# File 'lib/core_ext.rb', line 52 def to_bytehex map{|x| x.to_bytehex} end |
#to_sint ⇒ Object
31 32 33 34 |
# File 'lib/core_ext.rb', line 31 def to_sint sign = (self.last & 0x80 != 0) ? (-1 ^ ((1 << ((self.size * 8) - 1)) - 1)) : 0 sign | self.to_uint end |
#to_uint ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/core_ext.rb', line 14 def to_uint int = 0 self.each_with_index do |byte, index| int |= (byte << (index * 8)) end int end |
#xor(array2) ⇒ Object
48 49 50 |
# File 'lib/core_ext.rb', line 48 def xor(array2) zip(array2).map{|x, y| x ^ y } end |