Question about binary file reading

John Machin sjmachin at lexicon.net
Wed Mar 4 19:37:30 EST 2009


On Mar 5, 10:51 am, "Rhodri James" <rho... at wildebst.demon.co.uk>
wrote:
> On Wed, 04 Mar 2009 23:28:32 -0000, Tino Wildenhain <t... at wildenhain.de>  
> wrote:
>
>
>
> > Rhodri James wrote:
> >> On Wed, 04 Mar 2009 22:58:38 -0000, vibgyorbits <bka... at gmail.com>  
> >> wrote:
>
> >>> I'm writing a tool to do some binary file comparisons.
> >>> I'm opening the file using
>
> >>> fd=open(filename,'rb')
>
> >>> # Need to seek to 0x80 (hex 80th) location
>
> >>> fd.seek(0x80)
>
> >>> # Need to read just 8 bytes and get the result back in hex format.
> >>> x=fd.read(8)
> >>> print x
>
> >>> This prints out garbage. I would like to know what am i missing here.
>
> >> Your bytes are being interpreted as characters when you print the
> >> buffer, and the chance of them being meaningful text is probably small.
> >> Try the following:
>
> >> for b in x:
> >>     print hex(ord(b))
>
> > better:
>
> > print x.encode("hex")
>
> Encodings make my head hurt :-)  While there are programmatic purposes
> I'd leap at the "hex" encoder for, it doesn't make for the most human-
> readable output.  I'll stick with the for loop, if you don't mind.

One byte per line??

 >>> x = open('foo.xls', 'rb').read(8)
 >>> ' '.join(z.encode('hex') for z in x)
 'd0 cf 11 e0 a1 b1 1a e1'
 >>> ' '.join(z.encode('hex') for z in x).upper()
 'D0 CF 11 E0 A1 B1 1A E1'
 >>>




More information about the Python-list mailing list