lunixbochs / struc

Better binary packing for Go
MIT License
564 stars 42 forks source link

Pack and unpack array of "Structs" #59

Open ayh20 opened 6 years ago

ayh20 commented 6 years ago

I found an strange error when working with an array of structs.

Pack and unpack work correct as far as i can see with one issue ... it ignores the Endian setting (little).

For example unpack a byte stream that contains little endian floats ... type LEData struct { X float32 struc:"float32,little" // world co-ordinates of vehicle Y float32 struc:"float32,little" // world co-ordinates of vehicle Z float32 struc:"float32,little" // world co-ordinates of vehicle } unpackedData := &LEData{} err = struc.Unpack(buf2, unpackedData)

Data is correct ... However ...

The data is actually an array of these items...

unpackedData := &[2]LEData{} err = struc.Unpack(buf2, unpackedData)

The unpack "works" no errors at least :-) ..... but the data has been interpreted as Big Endian even though the data is tagged as Little.

Let me know if you need a working test case....

lunixbochs commented 6 years ago

Please do send a test case.

You can try struc.UnpackWithOptions(r, i, &struc.Options{Order: binary.LittleEndian})

Can you try unpacking a slice instead of array? like unpackedData[:]

lunixbochs commented 6 years ago

Also the syntax &[2]LEData{} is a little silly, you should probably remove the & and think about generally passing around a slice instead of pointer to array.

On Apr 11, 2018, at 6:14 AM, Andy Hampshire notifications@github.com wrote:

I found an strange error when working with an array of structs.

Pack and unpack work correct as far as i can see with one issue ... it ignores the Endian setting (little).

For example unpack a byte stream that contains little endian floats ... type LEData struct { X float32 struc:"float32,little" // world co-ordinates of vehicle Y float32 struc:"float32,little" // world co-ordinates of vehicle Z float32 struc:"float32,little" // world co-ordinates of vehicle } unpackedData := &LEData{} err = struc.Unpack(buf2, unpackedData)

The data is actually an array of these items...

unpackedData := &[2]LEData{} err = struc.Unpack(buf2, unpackedData)

The unpack "works" no errors at least :-) ..... but the data has been interpreted as Big Endian even though the data is tagged as Little.

Let me know if you need a working test case....

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

ayh20 commented 6 years ago

Not sure how it got like that in the first place ;-)

I'll try the Options syntax and create you a small test case, as soon as i have time.