I’m working on the parsing a binary file header. The structure looks like Adummy
.
First, I tried to use the reinterpret
but some values (p9 and p10) were incorrect.
If I sequentially read the data one by one, I got correct values.
Is this an expected behavior or, this is a bug?
using StaticArrays
struct Adummy
p1::SVector{4, UInt8}
p2::SVector{2, UInt8}
p3::UInt16
p4::UInt32
p5::UInt32
p6::UInt32
p7::UInt32
p8::UInt8
p9::UInt32
p10::UInt32
end
src = [
80, 67, 79, 32,
3, 0,
33, 0,
10, 0, 0, 0,
12, 0, 0, 0,
4, 0, 0, 0,
3, 0, 0, 0,
32,
33, 0, 0, 0,
80, 0, 0, 0,
43, 0, 0,
]
ioBuff = IOBuffer(UInt8.(src))
## read sequentially
seekstart(ioBuff)
seSt = []
for T in fieldtypes(Adummy)
push!(seSt, read(ioBuff, T))
end
## reinterpret data
seekstart(ioBuff)
data = read(ioBuff, sizeof(Adummy))
reSt = reinterpret(Adummy, data)[1]
## show results
for (n, val) in zip(propertynames(reSt), seSt)
v = getproperty(reSt, n)
println("$n should be $val but $v")
end
>> p1 should be UInt8[0x46, 0x44, 0x49, 0x46] but UInt8[0x46, 0x44, 0x49, 0x46]
>> p2 should be UInt8[0x03, 0x00] but UInt8[0x03, 0x00]
>> p3 should be 33 but 33
>> p4 should be 10 but 10
>> p5 should be 12 but 12
>> p6 should be 4 but 4
>> p7 should be 3 but 3
>> p8 should be 32 but 32
>> p9 should be 33 but 20480
>> p10 should be 80 but 11008