How to Convert a Hexadecimal String to Bytes in Python

Recently, I was working on a project where I had to process some raw data coming from a hardware device in the US supply chain. The device was sending information in hexadecimal string format, and I needed to convert it into bytes so that I could store and analyze it properly.

At first, this looked tricky because hexadecimal strings are just text, while bytes are raw binary data. But once I explored Python’s built-in tools, I realized there are multiple simple ways to handle this conversion.

In this article, I’ll show you the most practical methods I use to convert a hexadecimal string to bytes in Python. Each method is easy, and I’ll share full code examples so you can try them out immediately.

Method 1: Use Python’s 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.

Code:

import binascii

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

Output

Bytes: b'Python Programming'

You can see the output in the screenshot below.

How to convert hexadecimal string to bytes in Python

I’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

Method 2: Use bytes.fromhex() function

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

Code

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

Output

Bytes: b'United States of America '

You can see the output in the screenshot below.

python convert hex string to bytes

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

Method 3: Use bytearray.fromhex() Function in Python

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

Code

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

Output

Bytearray: bytearray(b'California')

You can see the output in the screenshot below.

python hex string to byte array

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

Method 4: Use 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)

Output

Bytes: b'New York'

You can see the output in the screenshot below.

How to Convert Hex String to Bytes in Python

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

Method 5: Use Python 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)]

Output

Bytes: b'Hello World'

You can see the output in the screenshot below.

Hex String to Bytearray in python

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

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:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.