all Python 3binasciifunction
binascii.unhexlify()
hex representation
The inverse of hexlify: converts hexadecimal text (str or bytes) to bytes: '01ff' → b'\x01\xff'. An odd length or non-hex characters raise binascii.Error.
Syntax
binascii.unhexlify(hexstr)
Spaces between pairs are not allowed: '01 ff' raises, whereas bytes.fromhex skips spaces, which makes fromhex friendlier for human input. The error is binascii.Error, a ValueError subclass, so except ValueError catches it too. Letter case does not matter: 'FF' equals 'ff'.
Examples
import binascii
print(binascii.unhexlify('01FF')) # → b'\x01\xff'
print(bytes.fromhex('01 ff')) # → b'\x01\xff'
try:
binascii.unhexlify('01 ff')
except ValueError as e:
print(type(e).__name__) # → Error
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy