How to use binascii.hexlify() to convert Byte Array to Hex String In Python

The inbuilt function of hexlify can be used to perform this particular task. This function is recommended for this particular conversion as it is tailor-made to solve this specific problem. 

Python3




import binascii
 
# initializing list
test_list = [124, 67, 45, 11]
 
# printing original list
print("The original string is : " + str(test_list))
 
# using binascii.hexlify()
# Converting bytearray to hexadecimal string
res = binascii.hexlify(bytearray(test_list))
 
# printing result
print("The string after conversion : " + str(res))


Output:

The original string is : [124, 67, 45, 11]
The string after conversion : 7c432d0b

Python | Convert Bytearray to Hexadecimal String

Sometimes, we might be in a problem in which we need to handle unusual Datatype conversions. One such conversion can be converting the list of bytes(byte array) to the Hexadecimal string format in Python. Let’s discuss certain Methods in which this can be done. 

Similar Reads

Using format() + join()  to Convert Byte Array to Hex String

The combination of the above functions can be used to perform this particular task. The format function converts the bytes into hexadecimal format. “02” in format is used to pad required leading zeroes. The join function allows joining the hexadecimal result into a string....

Using binascii.hexlify() to convert Byte Array to Hex String

...

Using the bytes.hex() method to directly convert the bytearray to a hexadecimal string:

The inbuilt function of hexlify can be used to perform this particular task. This function is recommended for this particular conversion as it is tailor-made to solve this specific problem....

Using the struct module and the h format specifier:

...