How to use the 'hex' encoding in Python 3.2 or higher? How to use the 'hex' encoding in Python 3.2 or higher? python-3.x python-3.x

How to use the 'hex' encoding in Python 3.2 or higher?


You need to go via the codecs module and the hex_codec codec (or its hex alias if available*):

codecs.encode(b'\x12', 'hex_codec')

* From the documentation: "Changed in version 3.4: Restoration of the aliases for the binary transforms".


Yet another way using binascii.hexlify():

>>> import binascii>>> binascii.hexlify(b'\x12\x34\x56\x78')b'12345678'


Using base64.b16encode():

>>> import base64>>> base64.b16encode(b'\x12\x34\x56\x78')b'12345678'