I have a buffer with some data that have different bit sizes (field 8 bits, then field 4 bits, then field 9 bits ...).
I need to read this. It would be great if there was some kind of library that allowed you to read it using a pointer at the bit level rather than the byte level.
Copying the buffer into the structure is not an option, because after researching I will need to use #pragma pack() or something similar and will not be portable.
Any idea?
EDIT: I will try to explain the scale of my problem using an example:
field1: 8 bits --> ok, get first byte field2: 6 bits --> ok, second byte, and a mask field3: 4 bits --> gets harder, i have to get 2 bytes, apply 2 different masks, and compose field4 ... field 15: 9 bits ---> No idea of how to do it with a loop to avoid writing manually every single case
And the only solution I can think of is to copy to struct, pragma pack and go. But in previous questions I was told that this is not a good solution due to portability. But I am ready to hear a different opinion if it saves me.
source share