Filipe Teixeira wrote:
Hi.

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

f=open('file.bin','rb')
a=f.read()
f.close()

a in now a string full of hex representations in the form:

a[6]='\x14'
a[7]='\x20'

I would like to convert these hex representations to int, but this
(the most obvious way) doesn't seem to be working

q=a[6]
q
'\x14'
int(q,16)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: invalid literal for int():

How can I do this?

Thanks

As you say you are trying to recover information from the old computer are you sure you want to convert the data to it's integer representation ? What I mean is in this case the string will contain the actual data read from the file, not a hex representation. It's just the python displays this data as '\x14'. BTW - are you sure it's displaying '\x20' from your data as this should be a space character i.e. " ".

In the python interpreter try this:

        mydata=""
        for i in range(256):
                mydata+=chr(i)
>> mydata

This will show you the hex representation of the values in mydata that it can't display however the string is not a string of "hex values" as such, it contains the binary values 0-255.
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to