如何将字节数组强制转换为Julia中的struct?
我是朱莉娅的新手。我正在尝试解析一个结构化二进制文件。我从文件中读取了n个字节,我想将字节数组强制转换为X类型的对象如何将字节数组强制转换为Julia中的struct?,julia,Julia,我是朱莉娅的新手。我正在尝试解析一个结构化二进制文件。我从文件中读取了n个字节,我想将字节数组强制转换为X类型的对象 struct X messageType::UInt8 second::UInt32 end f = open("myfile.bin") bytes = read(f, 5) 现在我想将字节强制转换为X的对象。我该如何做呢?您可以使用StructIO下面是方法 设置: using StructIO @io struct XX messag
struct X
messageType::UInt8
second::UInt32
end
f = open("myfile.bin")
bytes = read(f, 5)
现在我想将字节强制转换为X的对象。我该如何做呢?您可以使用
StructIO
下面是方法
设置:
using StructIO
@io struct XX
messageType::UInt8
second::UInt32
end align_packed
bytes = UInt8[0x72, 0xa3, 0x97, 0xcf, 0x64]
buf = IOBuffer(bytes)
现在运行代码:
julia> seekstart(buf); unpack(buf, XX)
XX(0x72, 0x64cf97a3)
julia> seekstart(buf); unpack(buf, XX, :BigEndian)
XX(0x72, 0xa397cf64)