Class: HrrRbSsh::DataType::Uint64

Inherits:
HrrRbSsh::DataType show all
Defined in:
lib/hrr_rb_ssh/data_type/uint64.rb

Overview

Uint64 provides methods to convert integer value and 64-bit unsigned binary string each other.

Class Method Summary collapse

Class Method Details

.decode(io) ⇒ ::Integer

Convert 64-bit unsigned binary into Integer value.

Parameters:

  • io (::IO)

    IO instance that has buffer to be read

Returns:

  • (::Integer)

    converted integer value



26
27
28
# File 'lib/hrr_rb_ssh/data_type/uint64.rb', line 26

def self.decode io
  (io.read(4).unpack("N")[0] << 32) + (io.read(4).unpack("N")[0])
end

.encode(arg) ⇒ ::String

Convert integer value into 64-bit unsigned binary string.

Parameters:

  • arg (::Integer)

    integer value to be converted

Returns:

  • (::String)

    converted 64-bit unsigned binary string

Raises:

  • (::ArgumentError)

    when arg is not between 0x0000_0000_0000_0000 and 0xffff_ffff_ffff_ffff



13
14
15
16
17
18
19
20
# File 'lib/hrr_rb_ssh/data_type/uint64.rb', line 13

def self.encode arg
  case arg
  when 0x0000_0000_0000_0000..0xffff_ffff_ffff_ffff
    [arg >> 32].pack("N") + [arg & 0x0000_0000_ffff_ffff].pack("N")
  else
    raise ArgumentError, "must be in #{0x0000_0000_0000_0000}..#{0xffff_ffff_ffff_ffff}, but got #{arg.inspect}"
  end
end