Class: BinaryCodec::Int32

Inherits:
Uint show all
Defined in:
lib/binary-codec/types/uint.rb

Instance Attribute Summary

Attributes inherited from SerializedType

#bytes

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Uint

#compare_to, from_parser, #initialize, width

Methods inherited from ComparableSerializedType

#compare_to, #eq, #gt, #gte, #lt, #lte

Methods inherited from SerializedType

from_bytes, from_hex, from_json, from_parser, get_type_by_name, #initialize, #to_byte_sink, #to_bytes, #to_hex, #to_json

Constructor Details

This class inherits a constructor from BinaryCodec::Uint

Class Method Details

.from(value) ⇒ Int32

Creates a new Int32 instance from a value.

Parameters:

  • value (Int32, Integer)

    The value to convert.

Returns:

  • (Int32)

    The created instance.



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/binary-codec/types/uint.rb', line 126

def self.from(value)
  return value if value.is_a?(self)
  if value.is_a?(Integer)
    # Ensure it fits in 32-bit signed
    if value < -2147483648 || value > 2147483647
      raise StandardError, "Value #{value} out of range for Int32"
    end
    # Convert to unsigned 32-bit for storage
    u_val = value < 0 ? value + 0x100000000 : value
    return new(int_to_bytes(u_val, 4))
  end
  super(value)
end

Instance Method Details

#value_ofInteger

Returns the numeric value of the Int32.

Returns:

  • (Integer)

    The signed 32-bit value.



118
119
120
121
# File 'lib/binary-codec/types/uint.rb', line 118

def value_of
  val = super
  val > 0x7FFFFFFF ? val - 0x100000000 : val
end