Retrieving int from hex in a file.

F

Filipe Teixeira

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
Traceback (most recent call last):

How can I do this?

Thanks
 
G

Gabriel Genellina

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):

Use ord(q)

py> help(ord)
Help on built-in function ord in module __builtin__:

ord(...)
ord(c) -> integer

Return the integer ordinal of a one-character string.

py>
 
P

Peter Otten

Filipe said:
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
 
B

bgeddy

Filipe said:
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)
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.
 
F

Filipe Teixeira

Use ord(q)

py> help(ord)
Help on built-in function ord in module __builtin__:

ord(...)
ord(c) -> integer

Return the integer ordinal of a one-character string.

py>

Thank you Gabriel. It fit's my purpose.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top