Parsing Binary Structures; Is there a better way / What is your way?

Dave Angel davea at ieee.org
Thu Aug 6 02:45:01 EDT 2009


Paul Rubin wrote:
> "Martin P. Hellwig" <martin.hellwig at dcuktec.org> writes:
>   
>> Is there an advantage using shifts and masks over my kitchen type solution?
>>     
>
> Weren't you complaining about the 8-to-1 expansion from turning each bit
> to an ascii char?
>
>   
One warning to Martin:

If you want your code portable across systems, watch out for 
big-endian/little-endian  issues, as well as alignment ones.  Shift & 
mask code tends to be highly specific to a particular endian-ness, 
especially if trying to get multiple bits that cross a byte or word 
boundary.

Over the years, I know I've seen at least three endian versions for the 
same 32bit word.  Something like abcd, dcba, and cdab.

One advantage of converting first to bitstrings, is that there's just 
the two places to fix, for portability, the conversion from byte array 
to bitstring, and the conversion back.

DaveA




More information about the Python-list mailing list