Open sn4k3 opened 2 years ago
Still struggle with this one... [FieldBitLength(24)] uint FieldName {get; set;}
does not return the correct value from 3 bytes as uint. Any suggestion?
Can you give me some more info? I just wrote a simple test that seems to work but I might be missing something.
Sure, here my definition:
What i get:
What i expect:
File is in BIG order and use a ton of uint24
Ahh it's big endian. Unfortunately there is a bug involving big endian and bit fields. I've spent hours trying to fix it but unfortunately it's incredibly complex. I need to at least add a check that will throw in these cases. Sorry about that, I'll try to get back to it soon!
Thanks for looking into it.
I've not look at source code, but one thing i always do when working with bytes and convertion is to avoid C# converter functions (BitConverter
), and do my own with bitwise. Those utility methods can return different outputs depending on machine endiness, unless you cover it with if (BitConverter.IsLittleEndian) Array.Reverse(bytes);
which is easy forgetable.
As much as I would like to blame something else, it is unfortunately all my own code! The issue has to do with assumptions I made when doing bit-packing early on. Unfortunately, fixing it will mean rewriting quite a bit of the underlying bit field assemblage, which gets complicated fast. If you're interested, most of the rabbit hole is contained in BoundedStream
.
@sn4k3 could you perhaps but together a couple of Test Cases for this in Github (within the FieldBitOffset / Endianess stuff probably makes sense).
I've had a bit of a dig through the BoundedStream stuff recently, so might be able to have a look at this problem also.
I have some situations where the format have uint's with 3 bytes, currently is it possible to define an uint and deserialize/serialize in 3 byte order? Making stream only to advance that 3 bytes so the next field will decode/encode at position 3 rather than 4?
PS: Should be cool to enable github discussions to use it as a forum board for questions instead the need to use issues for that purpose