all Python 3binasciifunction
binascii.a2b_hex()
hex representation
Converts hexadecimal notation to bytes, the same as unhexlify. An even number of hex digits is required, otherwise binascii.Error.
Syntax
binascii.a2b_hex(hexstr)
It accepts str and bytes, but str only if ASCII: Cyrillic input raises ValueError before parsing. The error message changed between versions (3.15 says 'Odd number of hexadecimal digits' instead of 'Odd-length string'), so tests must not compare it; catch the type. Since 3.15 the ignorechars parameter lets you skip spaces and colons.
Examples
import binascii
print(binascii.a2b_hex(b'cafe')) # → b'\xca\xfe'
try:
binascii.a2b_hex('abc')
except binascii.Error:
print('odd length') # → odd length
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy