Class: BinaryCodec::Int64

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) ⇒ Int64

Creates a new Int64 instance from a value.

Parameters:

  • value (Int64, Integer)

    The value to convert.

Returns:

  • (Int64)

    The created instance.



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/binary-codec/types/uint.rb', line 153

def self.from(value)
  return value if value.is_a?(self)
  if value.is_a?(Integer)
    if value < -9223372036854775808 || value > 9223372036854775807
      raise StandardError, "Value #{value} out of range for Int64"
    end
    u_val = value < 0 ? value + 0x10000000000000000 : value
    return new(int_to_bytes(u_val, 8))
  end
  super(value)
end

Instance Method Details

#value_ofInteger

Returns the numeric value of the Int64.

Returns:

  • (Integer)

    The signed 64-bit value.



145
146
147
148
# File 'lib/binary-codec/types/uint.rb', line 145

def value_of
  val = super
  val > 0x7FFFFFFFFFFFFFFF ? val - 0x10000000000000000 : val
end