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

Hendrik van Rooyen hendrik at microcorp.co.za
Thu Aug 6 03:58:30 EDT 2009


On Wednesday 05 August 2009 20:12:05 Paul Rubin wrote:
> "Martin P. Hellwig" <martin.hellwig at dcuktec.org> writes:
> > what I usually do is read the packet in binary mode, convert the
> > output to a concatenated 'binary string'(i.e. '0101011000110') and
>
> Something wrong with reading the data words as an integer and using
> old fashioned shifts and masks to get at the bit fields?

There is nothing wrong with that, and in the final analysis, it is what
is used - however, I can sympathize with the OP, as it is much nicer
to just call a named thing than to juggle a bunch of hard coded
shifts and masks.

I think the real point here is sort of meta programming - what
is needed is a way to describe and name the elements of the
structure so that useful python objects are created.

The code I posted for bits does that, and I use it all the
time - it is almost as good as being back in 8031 assembler
with direct named bit access.

- Hendrik



More information about the Python-list mailing list