# How to Convert Hexadecimal String to Bytes in Python[5 Methods]

Do you want to know “how to convert hexadecimal string to bytes in Python“? In this Python tutorial, we will explore 5 ways to convert hex to bytes in Python, each with its advantages and use cases.

Converting a hexadecimal string to bytes is a common operation in Python, especially when dealing with binary data or communicating with external systems.

By understanding these methods, you can handle hexadecimal string conversions effectively in various scenarios.

These are 5 different approaches to convert hexadecimal string to bytes in Python:

1. Using binascii.unhexlify()
2. Using bytes.fromhex()
3. Using bytearray.fromhex()
4. Using codecs.decode()
5. Using List Comprehension

## How to convert hexadecimal string to bytes in Python using binascii.unhexlify() function

We can use the unhexlify() method of the binascii module in Python, which is a convenient way to convert hexadecimal strings to bytes in Python. It returns the binary string that is represented by any hexadecimal string.

Consider the following example:

Code:

import binascii

hex_string = "507974686f6e2050726f6772616d6d696e67"
bytes_data = binascii.unhexlify(hex_string)
print("Bytes:", bytes_data)

In this code, we’ve defined the variable called hex_string that holds the hexadecimal numbers that use 16 different symbols (0-9 and A-F) to represent numbers. And using binascii.unhexlify() function to convert the hexadecimal string hex_string into binary data in Python.

READ:  How to Perform Word Count in Python Program

Output

Bytes: b'Python Programming'

Here’s a screenshot of my code to convert hexadecimal string to bytes in Python.

## Convert hex string to bytes in Python using bytes.fromhex() function

The bytes.fromhex() is a built-in method in Python that converts a hexadecimal string into its equivalent bytes object.

The benefit of using bytes.fromhex() is its simplicity and readability, providing a straightforward way to convert hexadecimal strings to bytes in Python.

Here is an example to convert hexadecimal string to bytes in Python using the fromhex() function.

Code

hex_string = "556e6974656420537461746573206f6620416d657269636120"
bytes_data = bytes.fromhex(hex_string)
print("Bytes:", bytes_data)

Output

Bytes: b'United States of America '

Attached is a screenshot of my Python code executed in Visual Studio.

## How to Convert Hex String to Bytes in Python using bytearray.fromhex() function

We can use bytearray.fromhex(), a method provided by the bytearray class in Python that converts a hexadecimal string into a mutable bytearray object.

It allows for in-place modification of the resulting bytearray, making it useful for situations where mutable binary data is needed.

Let’s see the practical example to convert hexadecimal string to bytes in Python using bytearray.fromhex() function.

Code

hex_string = "43616c69666f726e6961"
bytearray_data = bytearray.fromhex(hex_string)
print("Bytearray:", bytearray_data)

Output

Bytearray: bytearray(b'California')

Here’s a screenshot showcasing my Python code in Visual Studio.

## How to convert hex to byte in Python using codecs.decode() function

The decode() is a method provided by the codecs module in Python, which is used to decode data from one format to another. In this case, we’re using it to decode a hexadecimal string into its equivalent bytes.

Here’s a simple example:

Code

import codecs

hex_string = "4e657720596f726b"
bytes_data = codecs.decode(hex_string, "hex")
print("Bytes:", bytes_data)

We are using codecs.decode() function to decode the hexadecimal string into bytes, specifying “hex” as the decoding format.

READ:  How to copy a dictionary in Python (6 methods)

Output

Bytes: b'New York'

Displayed below is a screenshot of my Python code from Visual Studio.

## Converting hexadecimal string to byte value using List Comprehension

We use List comprehension in Python to iterate over the hexadecimal string, taking two characters at a time (representing one byte), converting them to integers using base 16 (hexadecimal), and storing them in a list.

Let’s see an example:

Code

hex_string = "48656c6c6f20576f726c64"
bytes_data = [int(hex_string[i:i+2], 16) for i in range(0, len(hex_string), 2)]
print("Bytes:", bytes(bytes_data))

In this code, we convert each pair of characters into hexadecimal in Python.
These integers are stored in a list called bytes_data.

[int(hex_string[i:i+2], 16) for i in range(0, len(hex_string), 2)]

Finally, it converts the list of integers into a bytes object using the bytes() constructor and prints it out.

Output

Bytes: b'Hello World'

Shown here is a screenshot capturing my Python code in Visual Studio.

## Conclusion

Here, we explored five different methods, like using binascii.unhexlify(), bytes.fromhex(), bytearray.fromhex(), codecs.decode(), and list comprehension to convert hexadecimal string to bytes in Python. Each method has its strengths and is useful in different situations.

By understanding and utilizing these methods, developers can effectively manipulate strings to suit their needs in various real-world scenarios.

You may also like to read the following articles in Python: