Convert Hex to String in Python

Python hex to String

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

Method 2: Using binascii module

Method 3: Using codecs module

Method 4: Using List comprehension

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.

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.

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

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.

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.

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.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *