Question about binary file reading

Chris Rebert clp2 at rebertia.com
Wed Mar 4 18:15:28 EST 2009


On Wed, Mar 4, 2009 at 2:58 PM, vibgyorbits <bkajey at gmail.com> wrote:
> 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

-- 
I have a blog:
http://blog.rebertia.com



More information about the Python-list mailing list