如何在Crystal中连接字节

时间:2018-06-03 01:46:11

标签: crystal-lang

我正在测试有关字节或切片的序列化,只是学习和尝试。我想在一个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)

2 个答案:

答案 0 :(得分:5)

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应该用于"连接字节"总的来说 - 只需写信给它。

答案 1 :(得分:0)

这不是您问题的答案,但是当我尝试连接实际的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,因此在示例中省略了