[Tutor] newb - Problem reading binary files

Alan Gauld alan.gauld at btinternet.com
Sat May 12 02:20:39 CEST 2007

"Elizabeth Finn" <finnfamilyus at yahoo.com> wrote 

> binary format, and then convert some of the values into 
> integer values. These values can range from 1 to 4 bytes.

See the struct module, and for an exampole the binary file 
sidebar in the file handling topic of my tutor. But caveat: 
struct needs to know the sizes and types of data you are 
reading in advance.
> .... for some of the 2-byte numbers I get only the first byte 
> and first half of the second byte - for instance: 'x06\x33' 
> comes out as 'x063'. This is very confusing 

Look closely and you will see that Python is telling you 
that the 06 represents the hex value of the byte. The second 
byte has no \x so is a character. The ASCII value of 0x33 
is '3'...

>>> hex(ord('3'))

> ...but "06 52" becomes "065". Any ideas?

Not on that one... x52 is 'R'... and chr(52) is '4'

Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list