Method: MemoryIO::Util.pack

Defined in:
lib/memory_io/util.rb

.pack(val, b) ⇒ String

Pack an integer into b bytes. Little endian is used.

Examples:

Util.pack(0x123, 4)
#=> "\x23\x01\x00\x00"

Parameters:

  • val (Integer)

    The integer to pack. If val contains more than b bytes, only lower b bytes in val will be packed.

  • b (Integer)

Returns:

  • (String)

    Packing result with length b.



129
130
131
# File 'lib/memory_io/util.rb', line 129

def pack(val, b)
  Array.new(b) { |i| (val >> (i * 8)) & 0xff }.pack('C*')
end