Class: Simms::Bytes
- Inherits:
-
Object
- Object
- Simms::Bytes
- Defined in:
- lib/simms/bytes.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
Instance Method Summary collapse
- #[](*args) ⇒ Object
- #bool(pos) ⇒ Object
- #calc_checksum ⇒ Object
- #count ⇒ Object
- #empty? ⇒ Boolean
- #float(pos) ⇒ Object
-
#initialize(byte_array) ⇒ Bytes
constructor
A new instance of Bytes.
- #sint16(pos) ⇒ Object
- #sint32(pos) ⇒ Object
- #slice(*args) ⇒ Object
- #to_s ⇒ Object
- #uint16(pos) ⇒ Object
- #uint32(pos) ⇒ Object
- #uint8(pos) ⇒ Object
Constructor Details
#initialize(byte_array) ⇒ Bytes
Returns a new instance of Bytes.
12 13 14 15 |
# File 'lib/simms/bytes.rb', line 12 def initialize(byte_array) @data = byte_array @data = @data.to_s.scan(/../) unless @data.is_a?(Array) end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
10 11 12 |
# File 'lib/simms/bytes.rb', line 10 def data @data end |
Instance Method Details
#[](*args) ⇒ Object
60 61 62 |
# File 'lib/simms/bytes.rb', line 60 def [](*args) Bytes.new(@data[*args]) end |
#bool(pos) ⇒ Object
45 46 47 |
# File 'lib/simms/bytes.rb', line 45 def bool(pos) @data[pos].hex == 1 end |
#calc_checksum ⇒ Object
64 65 66 |
# File 'lib/simms/bytes.rb', line 64 def calc_checksum @data.inject(0) {|sum, byte| sum + byte.hex} % 0xFF end |
#count ⇒ Object
17 18 19 |
# File 'lib/simms/bytes.rb', line 17 def count @data.count end |
#empty? ⇒ Boolean
21 22 23 |
# File 'lib/simms/bytes.rb', line 21 def empty? count == 0 end |
#float(pos) ⇒ Object
49 50 51 52 53 54 |
# File 'lib/simms/bytes.rb', line 49 def float(pos) v = uint32(pos) x = (v & ((1 << 23) - 1)) + (1 << 23) * (v >> 31 | 1) exp = (v >> 23 & 0xFF) - 127 return x.to_f * (2 ** (exp - 23)) end |
#sint16(pos) ⇒ Object
33 34 35 |
# File 'lib/simms/bytes.rb', line 33 def sint16(pos) [@data[pos...pos+2].join].pack('H*').unpack('s').first end |
#sint32(pos) ⇒ Object
41 42 43 |
# File 'lib/simms/bytes.rb', line 41 def sint32(pos) [@data[pos...pos+4].join].pack('H*').unpack('l').first end |
#slice(*args) ⇒ Object
56 57 58 |
# File 'lib/simms/bytes.rb', line 56 def slice(*args) Bytes.new(@data.slice(*args)) end |
#to_s ⇒ Object
68 69 70 |
# File 'lib/simms/bytes.rb', line 68 def to_s @data.join('') end |
#uint16(pos) ⇒ Object
29 30 31 |
# File 'lib/simms/bytes.rb', line 29 def uint16(pos) @data[pos...pos+2].reverse.join('').hex end |
#uint32(pos) ⇒ Object
37 38 39 |
# File 'lib/simms/bytes.rb', line 37 def uint32(pos) @data[pos...pos+4].reverse.join('').hex end |
#uint8(pos) ⇒ Object
25 26 27 |
# File 'lib/simms/bytes.rb', line 25 def uint8(pos) @data[pos].hex end |