How to Convert Hex to String in Python

3 Best Ways How to Convert Hex to String in Python

In this tutorial, I am going to demonstrate how to convert hex to string in Python.

Converting a value from one type to another is often a necessary evil. Say you have a value in hexadecimal format and want to convert it to a standard string, for example.

As you might already know, programming languages have their own native data types. For instance, in Python, strings are represented as sequences of characters called values.

The value can be anything from a space character to an entire sentence. Strings are also immutable – once they’re created, they cannot be changed again.  

Converting hexadecimal values requires special care because it could result in the loss of some digits or not even result in any loss of precision at all.

Hexadec is also case-sensitive, which makes conversions harder than they need to be.

Fortunately, there are ways through which you can convert your hexadecimal values into readable strings without losing too much information along the way.

In this tutorial, you will learn how to convert hexadecimal numbers to strings in Python.

How to Convert Hex to String in Python

To convert hey to string in Python use the int() method:

hexToString = int(testString, 16)

Converting Hex to String in Python

Hexadeccsv conversion helps us specify the value of a variable in base 16, where each digit represents 4 bits.

Converting Hex to String Using the int() method in Python

The int() function is a naive Python method for converting Hex to string. The int() function converts a hexadecimal string to the base 16; it will also transform the string into an integer simultaneously.

testString = 'A'
print ("The given string is: " + str(testString))
#Converting the hexadecimal string to a decimal string
hexToString = int(testString, 16)
print ("The converted hexadecimal string into a decimal string is: " + str(hexToString))

Output:

The given string is: A
The converted hexadecimal string into a decimal string is: 10

Converting Hex to String Using codecs.decode in Python

Codecs is a Python library that you can install anytime, and it’s codecs.decode() function to convert Hex to string in Python. This function takes two arguments. One is the bytes object, and another is encoding. Let me show you how you can convert a string to bytes using utf-8.

import codecs
hexString = "68656c6c6f"
stringBytes = bytes(hexString, encoding='utf-8')
binaryString = codecs.decode(stringBytes, "hex")
print(str(binaryString, 'utf-8'))

Output:

hello

1. Converting Hex to String Using Append Hex to String in Python

As you already know, a Hex string may start with 0x – you can slice the starting two values from the hex string. You can create a function that does it for you with the help of fromhex() function. Later you can concatenate the string with another one.

def hexToString(hex):
if hex[:2] == '0x':
    hex = hex[2:]
string_value = bytes.fromhex(hex).decode('utf-8')
return string_value
my_string = "This is: " + hexToString('0x68656c6c6f')
print(my_string)

Output:

This is: hello

2. Hex to String Using the literal_eval() Method in Python

You can feasibly convert a hexadecimal string to a decimal string using the literal_eval() function. This function comes with Abstract Syntax Trees or ast library.

It is a pretty simple function that you’d love to handle. You need to pass one argument through the function, which is the hexadecimal string only. That’s it! It will explicitly convert your hexadecimal string to a decimal string.

from ast import literal_eval
myString = "0x68656c6c6f"
print ("The Hexadecimal string is:", myString)
decimalString = literal_eval(myString)
print("The Decimal string is:", decimalString)

Output:

The Hexadecimal string is: 0x68656c6c6f
The Decimal string is: 448378203247

3. Hex to String Using bytes.fromhex() and bytes.decode() in Python

You can also use the bytes.fromhex() function to convert your hexadecimal string to bytes and then convert it to ASCII using the bytes.decodes() function. Before that, you need to slice the hexadecimal notation from the string. Here you go.

#Slicing the hexadecimal string
hexString = "0x68656c6c6f"[2:]
#Converting to byte object
bytes_object = bytes.fromhex(hexString)
#Converting to ASCII representation
asciiString = bytes_object.decode("ASCII")
print(asciiString)

Output: hello