# Convert Hex to String in Python | 6 Methods with Code

Data often comes in various formats, and it's essential to convert data between them. Hexadecimal (hex) is one such format frequently encountered, especially when dealing with binary data or low-level programming tasks.

In Python, converting hexadecimal to a string is a fundamental operation, and in this article, we will explore different methods and techniques to perform this conversion efficiently.

Hexadecimal, often called "hex," is a base-16 numeral system. Unlike the decimal system, which is base-10 and uses ten distinct digits (0-9), the hexadecimal uses sixteen distinct digits (0-9 and A-F). Each digit in a hexadecimal number represents four bits of binary data, making it a convenient choice for representing binary data in a more human-readable form.

For example, the decimal 42 is represented as "2A" in hexadecimal. In this representation, '2' corresponds to 2 * 16^0, and 'A' corresponds to 10 * 16^1. When converting hexadecimal to a string, we want to translate these hexadecimal values into their corresponding characters.

### Method 1: Using the bytes.fromhex() Method

Python offers a built-in method called fromhex() for converting a hex string into a bytes object. We can use this method to convert hex to string in Python.

Here's an example:

```hex_string = "57656c636f6d6520746f204661765475746f72"

bytes_obj = bytes.fromhex(hex_string)
result_string = bytes_obj.decode('utf-8')

print(result_string)
```

Output:

```Welcome to FavTutor
```

In this example, we first define the hexadecimal string, then use `bytes.fromhex()` to create a bytes object, and finally, we decode it into a string using the UTF-8 encoding.

### Method 2: Using List Comprehension

Another way to convert a hexadecimal string to a string in Python is by using list comprehension. This method gives you more control over the conversion process and can be useful in situations where you need to process the hex values individually.

```hex_string = "48656c6c6f2c20576f726c64"

result_string = ''.join([chr(int(hex_string[i:i+2], 16)) for i in range(0, len(hex_string), 2)])

print(result_string)
```

Output:

```Hello, World
```

In this example, we iterate through the hexadecimal string in pairs (i.e., two characters at a time), convert them to integers using int(hex, 16), and then use chr() to obtain the corresponding character. We join all the characters together to form the final string.

### Method 3: Using the codecs.decode() Function

The codecs module in Python provides a versatile way to perform various encoding and decoding operations. We can leverage this module to decode a hexadecimal string into a regular string.

```import codecs

hex_string = "48656c6c6f2c20576f726c64"

result_string = codecs.decode(hex_string, 'hex').decode('utf-8')

print(result_string)  # Output: Hello, World
```

In this code snippet, we first use `codecs.decode()` to decode the hexadecimal string, and then we decode the resulting bytes object into a string using the UTF-8 encoding.

### Method 4: Using binascii.unhexlify()

The binascii module in Python provides various functions for working with binary data, including a function called unhexlify(), which can be used to convert a hexadecimal string into bytes.

```import binascii

hex_string = "48656c6c6f2c20576f726c64"

bytes_obj = binascii.unhexlify(hex_string)
result_string = bytes_obj.decode('utf-8')

print(result_string)  # Output: Hello, World
```

Here, we use `binascii.unhexlify()` to convert the hexadecimal string into a bytes object and then decode it into a string as we did in Method 1.

### Method 5: Using bytearray.fromhex()

The bytearray class in Python provides an alternative way to convert a hexadecimal string into bytes.

```hex_string = "48656c6c6f2c20576f726c64"

byte_array = bytearray.fromhex(hex_string)
result_string = byte_array.decode('utf-8')

print(result_string)  # Output: Hello, World
```

In this example, we create a bytearray from the hexadecimal string using `bytearray.fromhex()` and then decode it into a string.

### Method 6: Using a Custom Function

We can also create a custom function based on our needs and necessities, here's a simple example:

```def hex_to_string(hex_string):
result = ""
for i in range(0, len(hex_string), 2):
hex_pair = hex_string[i:i+2]
decimal_value = int(hex_pair, 16)
char = chr(decimal_value)
result += char
return result

hex_string = "5468616e6b7320666f722063686f6f73696e67204661765475746f72"

result_string = hex_to_string(hex_string)

print(result_string)
```

Output:

```Thanks for choosing FavTutor
```

In this custom function, we iterate through the input hexadecimal string, extract pairs of characters, convert them to decimal values, and then use `chr()` to obtain the corresponding character. We build the resulting string character by character.

## Conclusion

Converting a hexadecimal representation to a string in Python is a common task in various programming scenarios. Depending on your requirements and preferences, you can choose from several methods to perform this conversion efficiently. Whether you opt for built-in functions or decide to create a custom solution, understanding these techniques will empower you to work effectively with hexadecimal data in your Python projects.

### FavTutor - 24x7 Live Coding Help from Expert Tutors! 