module IO::ByteFormat
¶
Defines a byte format to encode integers and floats
from/to Bytes
and IO
.
Decode from bytes¶
bytes = Bytes[0x34, 0x12]
int16 = IO::ByteFormat::LittleEndian.decode(Int16, bytes)
int16 # => 0x1234_i16
Decode from an IO¶
io = IO::Memory.new(Bytes[0x34, 0x12])
int16 = io.read_bytes(Int16, IO::ByteFormat::LittleEndian)
int16 # => 0x1234_i16
Encode to bytes¶
raw = uninitialized UInt8[2]
IO::ByteFormat::LittleEndian.encode(0x1234_i16, raw.to_slice)
raw # => StaticArray[0x34, 0x12]
Encode to IO¶
io = IO::Memory.new
io.write_bytes(0x1234_i16, IO::ByteFormat::LittleEndian)
io.to_slice # => Bytes[0x34, 0x12]