Class: Canoser::IntField
- Inherits:
-
Object
- Object
- Canoser::IntField
- Defined in:
- lib/canoser/field.rb
Constant Summary collapse
- @@pack_map =
{8 => "C", 16 => "S", 32 => "L", 64 => "Q"}
Instance Method Summary collapse
- #decode(cursor) ⇒ Object
- #decode_bytes(bytes) ⇒ Object
- #encode(value) ⇒ Object
-
#initialize(int_bits, signed = false) ⇒ IntField
constructor
A new instance of IntField.
- #inspect ⇒ Object
- #max_value ⇒ Object
- #pack_str ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(int_bits, signed = false) ⇒ IntField
Returns a new instance of IntField.
6 7 8 9 |
# File 'lib/canoser/field.rb', line 6 def initialize(int_bits, signed=false) @int_bits = int_bits @signed = signed end |
Instance Method Details
#decode(cursor) ⇒ Object
37 38 39 40 |
# File 'lib/canoser/field.rb', line 37 def decode(cursor) bytes = cursor.read_bytes(@int_bits/8) decode_bytes(bytes) end |
#decode_bytes(bytes) ⇒ Object
33 34 35 |
# File 'lib/canoser/field.rb', line 33 def decode_bytes(bytes) bytes.unpack(pack_str)[0] end |
#encode(value) ⇒ Object
29 30 31 |
# File 'lib/canoser/field.rb', line 29 def encode(value) [value].pack(pack_str) end |
#inspect ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/canoser/field.rb', line 11 def inspect if @signed "Int#{@int_bits}" else "Uint#{@int_bits}" end end |
#max_value ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/canoser/field.rb', line 42 def max_value if @signed 2**(@int_bits-1) - 1 else 2**@int_bits - 1 end end |
#pack_str ⇒ Object
23 24 25 26 27 |
# File 'lib/canoser/field.rb', line 23 def pack_str str = @@pack_map[@int_bits] str = str.downcase if @signed str end |
#to_s ⇒ Object
19 20 21 |
# File 'lib/canoser/field.rb', line 19 def to_s inspect end |