我正在测试使用字节或切片进行序列化,只是学习和尝试。我想在单个 10 字节字段中绑定 3 个参数,但我现在不知道如何在 Crystal 中连接它们或是否可能。我知道我可以通过创建数组或元组来实现这一点,但我想尝试是否可以将参数混合到单个缓冲区中。
例如,我想要一个混合 3 个参数的自描述二进制记录 ID:
类型 (UInt8( |类别 (UInt8( |微秒 (UInt64( = 总计 80 位 - 10 字节
type = 1_u8 # 1 byte
categ = 4_u8 # 1 byte
usec = 1527987703211000_u64 # 8 bytes (Epoch)
如何将所有这些变量连接到一个连续的 10 字节缓冲区中?
我想按索引检索数据,例如:
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
这显示了根据您的用例量身定制的示例,但IO::Memory
通常应该用于"连接字节"——只需写入它即可。
这不是您问题的答案,但是当我尝试连接实际Bytes[]
时,我一直在这里结束。Oleh 的答案仍然适用,但我尝试在这里给出一种更通用的方法:
# some bytes a
a = Bytes[131, 4, 254, 47]
# some other bytes b
b = Bytes[97, 98, 99]
# new buffer of sizes a and b
c = IO::Memory.new a.bytesize + b.bytesize
# without knowing the size of the slice, we just write byte by byte
a.each do |v|
# each byte can be represented by an u8
c.write_bytes UInt8.new v
end
# same for b
b.each do |v|
c.write_bytes UInt8.new v
end
# here you have your new bytes slice
c = c.to_slice
# => Bytes[131, 4, 254, 47, 97, 98, 99]
请注意,这仅适用于默认值IO::ByteFormat::LittleEndian
,因此在示例中省略