You are currently viewing Python Hex to String

How to convert hex to string in Python? You can convert a hexadecimal string to a regular string using the built-in functions of Python in a simple process. Use the bytes.fromhex() method to convert the hexadecimal string to bytes, and then decode the bytes using an appropriate character encoding.

Advertisements

You can convert hex to string in Python using many ways, for example, by using bytes.fromhex(), binascii, list comprehension, and codecs modules. In this article, I will explain various ways of converting a hexadecimal string to a normal string by using all these methods with examples.

1. Quick Examples of Converting Hexadecimal String to String

If you are in a hurry, below are some quick examples of how to convert hex to string.


# Quick examples of hex to string

import binascii
import codecs

# Initialization of hexadecimal string
hex_string = "57656c636f6d6520746f20537061726b42794578616d706c65"

# Example 1: Using bytes.fromhex() method
# to convert hexadecimal to normal string
byte_string = bytes.fromhex(hex_string)            
result = byte_string.decode('utf-8')

# Example 2: Using binascii.unhexlify() method
# To convert hexadecimal string to string
byte_string = binascii.unhexlify(hex_string)            
result = byte_string.decode('utf-8')  

# Example 3: Using list comprehension
# Convert hexadecimal string to normal string
byte_data = [int(hex_string[i:i+2], 16) for i in range(0, len(hex_string), 2)]
byte_string = bytes(byte_data)
result = byte_string.decode('utf-8')

# Example 4: Another example to convert hex to string 
# using list comprehension
result = ''.join([chr(int(hex_string[i:i+2], 16)) for i in range(0, len(hex_string), 2)])

# Example 5: Using codecs.decode() to convert
# hexadecimal string to string
byte_string = codecs.decode(hex_string, 'hex')
result = byte_string.decode('utf-8')

# Example 6: Using codecs.decode() method
# To convert hexadecimal string to normal string
byte_string = bytes(hex_string, encoding='utf-8')
binary_string = codecs.decode(byte_string, "hex")
result = str(binary_string, 'utf-8')

2. Using bytes.fromhex() Method

You can convert a hexadecimal string to a regular string using the bytes.fromhex() method. For example, first, initializes a hexadecimal string and converts it to bytes using bytes.fromhex(), and then decodes the bytes to a string using UTF-8 encoding. The final result will print on the console as a regular string.

Let’s take a hexadecimal string 57656c636f6d6520746f20537061726b42794578616d706c65 and convert it into a normal string i.e. Welcome to SparkByExample.


# Initialization of hexadecimal string
hex_string = "57656c636f6d6520746f20537061726b42794578616d706c65"

# Using bytes.fromhex() method
# To convert hexadecimal to bytes
byte_string = bytes.fromhex(hex_string)


# Decoding the bytes to a regular string using UTF-8 encoding               
result = byte_string.decode('utf-8') 
print("Hexa decimal string:", hex_string)      
print("After converting hex to string:",result)

Yields below output.

python hex string

3. Using Binascii Module

You can use the binascii module of Python to convert hexadecimal string to a regular string. This module provides various functions for working with binary data and hexadecimal encoding/decoding.

The unhexlify() function is one of the functions of this module, using this you can convert the hexadecimal string to bytes. The bytes are then decoded into a regular string using UTF-8 encoding. The final result will print on the console as a regular string.


import binascii 

# Initialization of hexadecimal string
hex_string = "57656c636f6d6520746f20537061726b42794578616d706c65"

# Using binascii.unhexlify() method
# To convert hexadecimal to bytes
byte_string = binascii.unhexlify(hex_string)

# Decoding the bytes to a regular string using UTF-8 encoding              
result = byte_string.decode('utf-8')
print("Hexa decimal string:", hex_string)       
print("After converting hex to string:",result)

Yields the same output as above.

4. Using List Comprehension

You can also use list comprehension to convert a hexadecimal string to a regular string. The list comprehension iterates over pairs of characters in the hexadecimal string and converts them to integers using base 16 (hexadecimal). The integers are then used to create a byte object. The bytes are decoded into a regular string using UTF-8 encoding.

However, keep in mind that while using list comprehension provides a more manual way to convert the hexadecimal string, using built-in functions like bytes.fromhex() or binascii.unhexlify() is generally more readable and idiomatic for this specific task.


# Initialization hexadecimal string
hex_string = "57656c636f6d6520746f20537061726b42794578616d706c65"

# Using list comprehension
# Convert hexadecimal to bytes
byte_data = [int(hex_string[i:i+2], 16) for i in range(0, len(hex_string), 2)]
byte_string = bytes(byte_data)

# Decoding the bytes to a regular string using UTF-8 encoding
result = byte_string.decode('utf-8')
print("Hexa decimal string:", hex_string)
print("After converting hex to string:", result)

# Another example to convert hex to string using list comprehension
result = ''.join([chr(int(hex_string[i:i+2], 16)) for i in range(0, len(hex_string), 2)])
print("After converting hex to string:", result)

Yields the same output as above.

5. Using Codecs Module

You can also use the codecs module in Python to convert a hexadecimal string to a regular string. For example, the codecs.decode() function directly decodes the hexadecimal string into bytes. Using UTF-8 encoding you can decode the bytes into a regular string.

The codecs module provides additional functionality for encoding and decoding data, and in this case, it simplifies the process of converting a hexadecimal string to bytes and then to a regular string.


import codecs

# Initialization hexadecimal string
hex_string = "57656c636f6d6520746f20537061726b42794578616d706c65"

# Using codecs.decode() to hexadecimal to bytes
byte_string = codecs.decode(hex_string, 'hex')

# Convert bytes to string
result = byte_string.decode('utf-8')
print("Hexa decimal string:", hex_string)
print("After converting hex to string:", result)

# Another example Using codecs.decode() method
# To convert hexadecimal to bytes
byte_string = bytes(hex_string, encoding='utf-8') 
binary_string = codecs.decode(byte_string, "hex")
result = str(binary_string, 'utf-8')
print("After converting hex to string:", result)

Yields the same output as above.

Conclusion

In this article, I have explained multiple ways of converting hexadecimal strings to regular strings in Python by using bytes.fromhex(), binascii, list comprehension, and codecs modules with examples.

Happy Learning !!