Question about binary file reading

Ben Finney bignose+hates-spam at
Thu Mar 5 01:10:14 CET 2009

I just found a well-hidden part of the behaviour you expected.

vibgyorbits <bkajey at> writes:

> # Need to read just 8 bytes and get the result back in hex format.
> print x

Why would this print the bytes in hex format? “Convert to hexadecimal”
is not the default text encoding for ‘print’.

Instead, use the built-in ‘hex’ function to create a new string with
the value of the bytes:

    bytes =
    for byte in bytes:
        print hex(byte)

If that's not sufficient, you'll need to be more explicit in
describing what it is you want to do.

 \         “Pinky, are you pondering what I'm pondering?” “I think so, |
  `\   Brain, but if it was only supposed to be a three hour tour, why |
_o__)   did the Howells bring all their money?” —_Pinky and The Brain_ |
Ben Finney

More information about the Python-list mailing list