What is binascii.unhexlify in Python?

binascii is a widely used Python library for ASCII-encoded binary representations. It contains several methods for converting to binary from ASCII or hex, and vice versa.

binascii.unhexlify is a method in the binascii library. It returns the binary string that is represented by any hexadecimal string.

The function unhexlify works opposite of the method b2a_hex(), or hexlify.

The data being converted must contain an even number of hex digits; otherwise it raises TypeError.

Code

import binascii
# simple command to see how unhexlify works
# hex value of A is 41
print(binascii.unhexlify('41'))
# data
x = "Hello"
# first convert string to bytes
res = bytes(x, 'utf-8')
# convert to hex
w = binascii.hexlify(res)
# unhexlify the hex
print(binascii.unhexlify(w))

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved