Question about binary file reading

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


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

vibgyorbits <bkajey at gmail.com> writes:

> # Need to read just 8 bytes and get the result back in hex format.
> x=fd.read(8)
> 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 = fd.read(8)
    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