Class: HrrRbSsh::DataType::Byte

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

Overview

Byte provides methods to convert integer value and 8-bit unsigned binary string each other.

Class Method Summary collapse

Class Method Details

.decode(io) ⇒ ::Integer

Convert 8-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/byte.rb', line 26

def self.decode io
  io.read(1).unpack("C")[0]
end

.encode(arg) ⇒ ::String

Convert integer value into 8-bit unsigned binary string.

Parameters:

  • arg (::Integer)

    integer value to be converted

Returns:

  • (::String)

    converted 8-bit unsigned binary string

Raises:

  • (::ArgumentError)

    when arg is not between 0x00 and 0xff



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

def self.encode arg
  case arg
  when 0x00..0xff
    [arg].pack("C")
  else
    raise ArgumentError, "must be in #{0x00}..#{0xff}, but got #{arg.inspect}"
  end
end