Re: Binary handling

From: Diez B. Roggisch (deetsNOSPAM_at_web.de)
Date: 05/17/04


Date: Mon, 17 May 2004 14:24:40 +0200

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


Relevant Pages

  • Re: tuples, index method, Pythons design
    ... I am using the struct module to get binary data from a file. ... def skipuntil: ... should use array.array, and voila!, you get an index method. ...
    (comp.lang.python)
  • Binary handling
    ... I have a file of binary data. ... I want to read this file and parse the data in ... The format of the file is: ... but I can't split the diferent fiels. ...
    (comp.lang.python)
  • Re: Reading binary from a file...
    ... In python I'm able to read in binary data from a file. ... I want generic python lists or tupels not numpy or numeric etc ... You really want the struct module here. ...
    (comp.lang.python)
  • Re: write sequential variable len record from PC
    ... Such records usually do not contain any binary data and are ... Set File is the only tool that I know of supplied by OpenVMS that can do ... Usually with variable format files, ... A PC is going to be using IEEE floating point or it is not going to be ...
    (comp.os.vms)
  • Which data serialization format?
    ... I am unsure which data serialization format to use. ... sending binary data over it could become quiet awkward. ... I have made some tests with the official Perl-Implementation of Thrift⁰ ...
    (comp.lang.perl.misc)