lib/canoser/field.rb in canoser-0.1.2 vs lib/canoser/field.rb in canoser-0.1.3

- old
+ new

@@ -1,24 +1,31 @@ module Canoser class IntField @@pack_map = {8 => "C", 16 => "S", 32 => "L", 64 => "Q"} - def initialize(int_bits) + def initialize(int_bits, signed=false) @int_bits = int_bits + @signed = signed end def inspect - "Uint#{@int_bits}" + if @signed + "Int#{@int_bits}" + else + "Uint#{@int_bits}" + end end def to_s inspect end def pack_str - @@pack_map[@int_bits] + str = @@pack_map[@int_bits] + str = str.downcase if @signed + str end def encode(value) [value].pack(pack_str) end @@ -31,17 +38,26 @@ bytes = cursor.read_bytes(@int_bits/8) decode_bytes(bytes) end def max_value - 2**@int_bits - 1 + if @signed + 2**(@int_bits-1) - 1 + else + 2**@int_bits - 1 + end end end Uint8 = IntField.new(8) Uint16 = IntField.new(16) Uint32 = IntField.new(32) Uint64 = IntField.new(64) + + Int8 = IntField.new(8, true) + Int16 = IntField.new(16, true) + Int32 = IntField.new(32, true) + Int64 = IntField.new(64, true) class Bool def self.encode(value) if value "\1"