Open In App

Convert Hex String to Bytes in Python

Last Updated : 05 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Hexadecimal strings are a common representation of binary data, especially in the realm of low-level programming and data manipulation. In Python, converting a hex string to bytes is a frequent task, and there are various methods to achieve this. In this article, we will explore some simple and commonly used methods for converting a hex string to bytes in Python.

Python Convert Hex String To Bytes

Below, are the ways to Python Convert Hex String To Bytes in Python.

  • Using bytes.fromhex() Function
  • Using bytearray.fromhex() Function
  • Using List Comprehension Function
  • Using binascii.unhexlify() Function

Convert Hex String To Bytes Using bytes.fromhex() Function

In this example, below code initializes a hex string "1a2b3c" and converts it to bytes using the `bytes.fromhex()` method, storing the result in the variable `bytes_result`. The subsequent print statements display the types of the original hex string, the resulting bytes, and the type of the bytes object.

Python3




hex_string = "1a2b3c"
bytes_result = bytes.fromhex(hex_string)
 
print(type(hex_string))
print(bytes_result)
print(type(bytes_result))


Output

<class 'str'>
b'\x1a+<'
<class 'bytes'>

Convert Hex String To Bytes Using bytearray.fromhex() Function

In this example, below code defines a hex string "1a2b3c" and converts it to a bytearray using the `bytearray.fromhex()` method, storing the result in the variable `bytearray_result`. The subsequent print statements display the type of the original hex string, the resulting bytearray, and the type of the bytearray object.

Python3




hex_string = "1a2b3c"
bytearray_result = bytearray.fromhex(hex_string)
 
print(type(hex_string))
print(bytearray_result)
print(type(bytearray_result))


Output

<class 'str'>
bytearray(b'\x1a+<')
<class 'bytearray'>

Convert Hex String To Bytes Using List Comprehension Function

In this example, below code initializes a hex string “1a2b3c” and converts it to bytes using a list comprehension and the `int()` function. The resulting bytes are stored in the variable `bytes_result`. The subsequent print statements display the type of the original hex string, the resulting bytes, and the type of the bytes object.

Python3




hex_string = "1a2b3c"
bytes_result = bytes([int(hex_string[i:i+2],
                          16) for i in range(0, len(hex_string), 2)])
 
print(type(hex_string))
print(bytes_result)
print(type(bytes_result))


Output

<class 'str'>
b'\x1a+<'
<class 'bytes'>

Convert Hex String To Bytes Using binascii.unhexlify() Function

In this example, below code uses the `binascii` module to convert the hex string "1a2b3c" to bytes using the `unhexlify()` function. The resulting bytes are stored in the variable `bytes_result`. The subsequent print statements display the type of the original hex string, the resulting bytes, and the type of the bytes object.

Python3




import binascii
 
hex_string = "1a2b3c"
bytes_result = binascii.unhexlify(hex_string)
 
print(type(hex_string))
print(bytes_result)
print(type(bytes_result))


Output

<class 'str'>
b'\x1a+<'
<class 'bytes'>

Conclusion

Converting a hex string to bytes in Python can be accomplished using various methods, and the choice of method often depends on the specific requirements of the task at hand. The methods presented in this article are simple, commonly used, and cover a range of scenarios. Depending on the context and the nature of the hex data, you can choose the method that best fits your needs.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads