Retrieving int from hex in a file.

Peter Otten __peter__ at web.de
Mon Apr 28 07:03:31 EDT 2008


Filipe Teixeira wrote:

> I have to open a binary file from an old computer and recover the
> information stored (or at least try to). I use:

> I would like to convert these hex representations to int, but this

If you want to do it efficiently have a look at the array module:

>>> import os, array
>>> a = array.array("B")
>>> filename = "/usr/bin/python"
>>> a.fromfile(open(filename, "rb"), os.path.getsize(filename))
>>> a[10]
0
>>> a[:10]
array('B', [127, 69, 76, 70, 2, 1, 1, 0, 0, 0])

Peter



More information about the Python-list mailing list