Reading a binary file

Andrew Bennetts andrew-pythonlist at puzzling.org
Thu Jun 26 10:33:05 CEST 2003


On Thu, Jun 26, 2003 at 10:01:06AM +0200, Sorin Marti wrote:
> Hi all,
> 
> I am quite new to python and very new to this list.
> 
> I've got following problem. I have a binary file which contains 
> information I should read. I can open the file with
> 
> f = open ('cpu1db2.dat', 'rb')
> 
> That's no problem. But now I need the hex values of the binary file.

You can get the hex value of a 1-character string with hex(ord(char)), e.g.:

    >>> char = 'a'
    >>> hex(ord(char))
    '0x61'

But I'm guessing that you might find the 'struct' module even more useful:
    http://python.org/doc/current/lib/module-struct.html

-Andrew.






More information about the Python-list mailing list