Question about binary file reading

V

vibgyorbits

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.
Basically, I am trying to read
8 bytes from location 0x80 from a binary file called "filename"

Any tips/inputs are welcome.

thanks!
 
C

Chris Rebert

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.

`print x` outputs the raw bytes in the bytestring `x` you just read,
which yes, generally looks like gibberish.
It doesn't magically convert it to hex format. Remember that
bytestrings could just as well contain ASCII in other situations,
which you certainly wouldn't want to see as hex. You'll have to
explicitly/manually do the conversion from bytes->hex.

Cheers,
Chris
 

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

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top