python3 binascii.hexlify ...

Peter Otten __peter__ at web.de
Sat Feb 9 06:10:36 EST 2013


Cameron Simpson wrote:

> This seems to return a bytes object in Python 3.3.0. I was expecting a
> string. The documentation here:
> 
>   http://docs.python.org/3/library/binascii.html#binascii.hexlify
> 
> also keeps me expecting a string. Am I missing something?

"""Return the hexadecimal representation of the binary data. Every byte of 
data is converted into the corresponding 2-digit hex representation.
"""

makes it pretty clear that the function is operating on bytes, not str.

The following sentence "The resulting string..." is likely a leftover from 
Python 2 and should be fixed.

If you need str instead of bytes it's easy enough to do the 
encoding/decoding yourself:

>>> import binascii as ba
>>> ba.hexlify("äöü".encode())
b'c3a4c3b6c3bc'
>>> ba.unhexlify(_).decode()
'äöü'





More information about the Python-list mailing list