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
.
import binascii# simple command to see how unhexlify works# hex value of A is 41print(binascii.unhexlify('41'))# datax = "Hello"# first convert string to bytesres = bytes(x, 'utf-8')# convert to hexw = binascii.hexlify(res)# unhexlify the hexprint(binascii.unhexlify(w))
Free Resources