Binary handling

Diez B. Roggisch deetsNOSPAM at web.de
Mon May 17 08:24:40 EDT 2004


Derfel wrote:

> Hello all,
> I have a file of binary data. I want to read this file and parse the data
> in it. The format of the file is:
> 8 bits number of data pakets
> 6 bits offset
> 20 bits time
> 8 states
> 
> How can I read this fields in binary? I see the struct module and the
> binascii, but I can't split the diferent fiels.

You can use struct to create a long from your data and then work on that
with the usual bitwise operators for shift and boolean and so that you
extract the actual data. If the above is the entire spec, I see some
problems to get the alignment proper, but with a bit of shifting you should
be able to work around that.


-- 
Regards,

Diez B. Roggisch



More information about the Python-list mailing list