I'm testing about serialization with bytes or slices, just learning and trying. I would like to bind 3 parameters in a single 10 bytes field, but I don't now how to concatenate them in Crystal or whether it is possible. I know I can achieve this by creating arrays or tuples, but I want to try whether it is possible to mix the parameters into a single buffer.
For instance, I want a self-descriptive binary record ID mixing 3 parameters:
Type (UInt8) | Category (UInt8) | Microseconds (UInt64) = Total 80 bits - 10 bytes
type = 1_u8 # 1 byte
categ = 4_u8 # 1 byte
usec = 1527987703211000_u64 # 8 bytes (Epoch)
How do I concatenate all this variables into a continuous 10 bytes buffer?
I want to retrieve the data by the index, like:
type = buff[0,1]
categ = buff[1,1]
usec = buff[2,8].to_u64 # (Actually not possible)
typ = 1_u8 # 1 byte
categ = 4_u8 # 1 byte
usec = 1527987703211000_u64 # 8 bytes (Epoch)
FORMAT = IO::ByteFormat::LittleEndian
io = IO::Memory.new(10) # Specifying the capacity is optional
io.write_bytes(typ, FORMAT) # Specifying the format is optional
io.write_bytes(categ, FORMAT)
io.write_bytes(usec, FORMAT)
buf = io.to_slice
puts buf
# --------
io2 = IO::Memory.new(buf)
typ2 = io2.read_bytes(UInt8, FORMAT)
categ2 = io2.read_bytes(UInt8, FORMAT)
usec2 = io2.read_bytes(UInt64, FORMAT)
pp typ2, categ2, usec2
Bytes[1, 4, 248, 99, 69, 92, 178, 109, 5, 0]
typ2 # => 1_u8
categ2 # => 4_u8
usec2 # => 1527987703211000_u64
This shows an example tailored to your use case, but IO::Memory
should be used for "concatenating bytes" in general -- just write to it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With