The Wayback Machine - https://web.archive.org/web/20230319205254/https://www.geeksforgeeks.org/python-convert-bytearray-to-hexadecimal-string/
Skip to content
Related Articles
Open in App
Not now

Related Articles

Python | Convert Bytearray to Hexadecimal String

Improve Article
Save Article
  • Last Updated : 19 Mar, 2023
Improve Article
Save Article

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. 

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. 

Python3




# initializing list
test_list = [124, 67, 45, 11]
 
# printing original list
print("The original string is : " + str(test_list))
 
# using join() + format()
# Converting bytearray to hexadecimal string
res = ''.join(format(x, '02x') for x in 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

Using binascii.hexlify() to convert Byte Array to Hex 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. 

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

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

Step by step Algorithm:

  1. Define a test_list,bytearray containing the bytes to be converted.
  2. Use the hex() method of the bytearray class to convert the bytearray to a hexadecimal string.
  3. Store the resulting hexadecimal string in a variable.
  4. Print the resulting string.

Python3




# define the original bytearray
test_list = [124, 67, 45, 11]
byte_array = bytearray(test_list)
 
print("The string before conversion: " + str(test_list))
# convert bytearray to hexadecimal string
hex_string = byte_array.hex()
 
# print the result
print("The string after conversion: " + hex_string)

Complexity Analysis:

Time Complexity: O(n), where n is the length of the input bytearray. The hex() method simply iterates over the bytes in the bytearray and converts each byte to its hexadecimal representation, which takes constant time per byte.

Space Complexity: O(n), since we are creating a new string object to store the resulting hexadecimal string.

RECOMMENDED ARTICLES – Python | bytearray() function


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!