I have a buffer with some data which come in different bit sizes (8 bits field, then 4 bits field, then a 9 bits field…).
I need to read it. It would be great if there where some library which allowed reading it using a pointer at bit level, and not a byte level.
Copying the buffer to a struct is not an option, because after researching I would need to use #pragma pack() or something similar, and would not be portable.
Any idea?
EDIT: I will try to explain the magnitude of my problem with 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 copying to an struct, pragma pack, and go. But I have been told in previous questions that it is not a good solution, because of portability. But I am willing to hear a different opinion if it rescues me.
Use bit manipulation: