Table of Contents
1. Introduction
The representation of data in Hexadecimal is commonly used in computer programming. It is used to represent binary data in a human-readable form, as understanding the machine language (i.e., Binary) is difficult for humans. Hexadecimal notation is used in networking protocols, e.g., IPv6 and cryptography, and widely used in graphics, e.g. to represent numbers. Note that hexadecimal uses 16
digits, including 0-9 and A-F
, to represent numbers.
Considering the importance of hexadecimal, some programming languages and APIs require string representation of binary data. As a lot of data is in hexadecimal form, so we need to convert the hexadecimal number to a string. This conversion helps to display and manipulate the binary
data as text
, which makes the task more convenient for humans.
In python language, there are different ways to convert Hex to String.
Method 1: Using
bytes.fromhex()
method
1234 byte_str = bytes.fromhex(hex_str) #Convert hex string to bytesregular_str = byte_str.decode('utf-8') #Convert bytes to regular stringMethod 2: Using
binascii
module
1234 byte_str = binascii.unhexlify(hex_str) # Convert hex string to bytesregular_str = byte_str.decode('utf-8') # Convert bytes to regular stringMethod 3: Using
codecs
module
1234 byte_str = binascii.unhexlify(hex_str) # Convert hex string to bytesregular_str = byte_str.decode('utf-8') # Convert bytes to regular stringMethod 4: Using List comprehension
123 regular_str = ''.join([chr(int(hex_str[i:i+2], 16)) for i in range(0, len(hex_str), 2)])
Let’s go through each method in detail.
2. Using bytes.fromhex() Method
Use the bytes.fromhex()
method to convert a hexadecimal string to a simple string in Python.
1 2 3 4 5 6 |
hex_str = "48656c6c6f20576f726c64" #Hex string byte_str = bytes.fromhex(hex_str) #Convert hex string to bytes regular_str = byte_str.decode('utf-8') #Convert bytes to regular string print(regular_str) #Print Output |
1 2 3 |
Hello World |
First, the hex_str
is defined, which contains a sequence of characters representing a hexadecimal number. After that, the bytes.fromhex()
method is called with hex_str
as its argument that converts the string of hexadecimal characters into a byte object. The resulting byte_str
variable contains the byte values represented by the hexadecimal characters.
The decode()
method is called on the byte_str
variable with 'utf-8'
as its argument. This method converted the bytes object into a regular string using the UTF-8 encoding. The resulting regular_str
variable contains the "Hello World"
string. Finally, the regular_str
variable is printed to the console using the print()
function.
3. Using binascii Module
The binascii
Module can be used for hex to String conversion. It follows the same steps as required for the above method. The main difference is at the point of converting a hexadecimal string to bytes.
1 2 3 4 5 6 7 |
import binascii #Import module hex_str = "48656c6c6f20576f726c64" # Hex string byte_str = binascii.unhexlify(hex_str) # Convert hex string to bytes regular_str = byte_str.decode('utf-8') # Convert bytes to regular string print(regular_str) # Print Output |
1 2 3 |
Hello World |
In this example, first, we imported the binascii
module, which provides functions for converting between binary and ASCII formats. Then, the unhexlify()
function converts the hex string to a bytes object. Finally, the bytes object is converted to a regular string using the decode()
method and printed to the console.
4. Using codecs Module
The codecs
‘s decode() method can be used for python hex to String conversion. It takes two arguments – bytes object and encoding type
1 2 3 4 5 6 7 8 |
import codecs #Import module hex_str = "48656c6c6f20576f726c64" hex_str_bytes = bytes(hex_str, encoding='utf-8') # Convert hex string to bytes binary_string = codecs.decode(hex_str_bytes, "hex") # Convert bytes to regular string print(str(binary_string, 'utf-8')) |
1 2 3 |
Hello World |
In this example, first, we imported the codecs
module. Then, the bytes()
function converts the hex string to a bytes object. Finally, the bytes object is converted to a regular string using the decode()
method and printed to the console.
Further reading:
5. Using List Comprehension
Use list comprehension to convert hexadecimal to string in Python. Unlike the above methods, the hexadecimal string is not converted into bytes but into a decimal integer.
1 2 3 4 5 6 7 8 9 10 11 |
#Hex string hex_str = "48656c6c6f20576f726c64" #loop over the hexadecimal string to convert to #a decimal integer and join the respective ASCII character regular_str = ''.join([chr(int(hex_str[i:i+2], 16)) for i in range(0, len(hex_str), 2)]) #Print Output print(regular_str) |
1 2 3 |
Hello World |
The list comprehension converted each pair of hexadecimal characters into a decimal integer using the int()
function for this code. Then, the integer was converted to its corresponding ASCII character using the chr()
function. Finally, joined the resulting list of characters into a single string using the join()
method.
Please note that this method works for hex strings that have even number of digits and do not have prefix 0x.
6. Conclusion
All four methods produced the same output: the string "Hello World"
as the input string was the same. Therefore, you can try different hexadecimal strings and convert them into regular strings using the above methods.