reading binary data from a 32 bit machine on 64 bit machine

MRAB google at mrabarnett.plus.com
Thu Feb 19 20:56:53 CET 2009


harijay wrote:
> Hi I am very confused with the use of the struct module to read binary
> data from a file.
> ( I have only worked with ascii files so far)
> 
> I have a file spec for a Data-logger (http://www.dataq.com/support/
> techinfo/ff.htm)
> 
> I am collecting some voltage , time traces on one channel and they are
> written to the binary file on a 32 bit windows machine
> 
> The file spec says that the number of header bytes in the data file
> header is stored as a 16 bit  eye "I" at bits 6-7
> 
> Now I want to get at that number. When I try format !h I get a
> meaningful number
> If f is my file handle opened with "rb" mode
> 
>>>> f.seek(5)
>>>> (Integer,) = struct.unpack('!h',f.read(2))
>>>> (Integer,)
> (9348,)
> 
> I am assuming that means that there are 9348 header bytes . Can
> someone look at the format spec and tell me if I am on the right
> track.
> 
> Also if a binary guru can point me in the direction of a single line
> format pythonic way to get at the header that will be great
> 
The number of header bytes is stored in bytes 6-7, so you need seek(6),
and the 16-bit value is little-endian as far as I can tell:

     f.seek(6)
     header_bytes, = struct.unpack('<h', f.read(2))



More information about the Python-list mailing list