- How to Convert Hex String to Bytes in Python?
- Method 1: Using bytes.fromhex(hex_string)
- Method 2: Using codecs.decode() Function
- Method 3: Using the binascii Module
- Conclusion
- How to Convert Hex String to Bytes in Python?
- Hex String to Bytes using bytes.fromhex(hex_string)
- Examples
- Convert Hex String with Prefix ‘0x’ to Bytes
- What’s the Difference Between bytearray and bytes?
- Where to Go From Here
How to Convert Hex String to Bytes in Python?
Data is often represented in hexadecimal format, which is a commonly used system for representing binary data in a human-readable form. In Python, the hex string is converted into bytes to work with binary data or to extract each value from the raw data. To convert the hex string into bytes, the bytes.from() and codecs.decode() functions are used.
This post provides various ways to convert hex string to bytes using the given below contents:
Method 1: Using bytes.fromhex(hex_string)
The “bytes.fromhex()” function is used to return a byte object (sequences of integers) by accepting the single hexadecimal value. The “bytes.fromhex(hex_string)” function is utilized in the following code to convert hex string to bytes:
hex_string = 'ff' print('Hex String Value: ',hex_string) output = bytes.fromhex(hex_string) print('Bytes Value: ',output) print(type(output))
In this example, the “bytes.fromhex()” function takes the hexadecimal strings “ff” as an argument and converts it into bytes object.
The given hex string has been converted into bytes.
Method 2: Using codecs.decode() Function
The “codecs.decode()” function is used to decode binary strings into normal form. In the below code the “codecs.decode()‘ function is used to convert hex string to bytes:
import codecs bytes_value = codecs.decode('ff', 'hex_codec') print(type(bytes_value)) print(bytes_value)
The “codecs.decode()” function converts the hex string into bytes by taking the hex string “ff” as a first argument and the encoding “hex_codec” as a second argument.
The given string has been converted into a bytes object.
Method 3: Using the binascii Module
In Python, the module named “binascii” is utilized to convert the binary representations into ASCII-encoded binary representations. The “binascii.unhexlify()” function of the binascii module is used in the below code to convert hex string to bytes:
import binascii hexa_string = '507974686f6e' bytes_value = binascii.unhexlify(hexa_string) print('Byte value of the string:', bytes_value)
The “binascii.unhexlify()” function takes the “hex string” as an argument and converts them into bytes.
The input hex string “507974686f6e” has been converted into bytes “Python.”
Conclusion
To convert hex strings to bytes, the “bytes.fromhex()” and “codecs.decode()” functions are used. Apart from that, the “binascii” module is also exercised in Python. The “bytes.fromhex()” function takes the hex string as an argument and converts it into bytes. Similarly, the “codecs.decode()” function decodes the hex string into bytes, and the “unhexlify()” function can also return the bytes of the input hex string. This guide presented various ways to convert hex strings to bytes in Python.
How to Convert Hex String to Bytes in Python?
Hex String to Bytes using bytes.fromhex(hex_string)
To convert a hexadecimal string to a bytes object, pass the string as a first argument into bytes.fromhex(hex_string) method. For example, bytes.fromhex(‘ff’) yields b’\xff’ .
hex_string = 'ff' print(bytes.fromhex(hex_string)) # b'\xff'
Examples
And here’s how you can convert the additional examples shown above:
>>> bytes.fromhex('01') b'\x01' >>> bytes.fromhex('0101') b'\x01\x01' >>> bytes.fromhex('04') b'\x04' >>> bytes.fromhex('01 0a') b'\x01\n' >>> bytes.fromhex('01 02 0e 0f 0f') b'\x01\x02\x0e\x0f\x0f' >>> bytes.fromhex('0f0f') b'\x0f\x0f' >>> bytes.fromhex('ff') b'\xff'
Convert Hex String with Prefix ‘0x’ to Bytes
If your hex string has a prefix ‘0x’ in front of it, you can convert it into a bytes object by using slicing operation hex_string[2:] to get rid of the prefix before converting it using bytes.fromhex(hex_string[2:]) .
hex_string = '0x0f' print(bytes.fromhex(hex_string[2:])) # b'\x0f'
If you don’t know slicing well enough in Python, feel free to check out my in-depth tutorial here:
A simple way to convert a hexadecimal string hex_string to a bytearray type is to use the bytearray.fromhex(hex_string) method. For example, bytearray.fromhex(‘deadbeef’) returns the bytearray(b’\xde\xad\xbe\xef’) type.
hex_string = 'deadbeef' print(bytearray.fromhex(hex_string)) # bytearray(b'\xde\xad\xbe\xef')
After reading this, you may wonder:
What’s the Difference Between bytearray and bytes?
The difference between bytearray and bytes types is that bytes is an immutable version of bytearray . So you can modify an object of the latter but not of the former.
For example, let’s create both types from the same hex_string as learned before:
hex_string = 'deadbeef' my_bytes = bytes.fromhex(hex_string) my_bytearray = bytearray.fromhex(hex_string)
Next, you’ll change the my_bytearray variable — no problem:
print(my_bytearray) # bytearray(b'\xde\xad\xbe\xef') # Modify bytearray is possible my_bytearray[0] = 3 print(my_bytearray) # bytearray(b'\x03\xad\xbe\xef')
But what happens if you try to modify the my_bytes variable? An error TypeError: ‘bytes’ object does not support item assignment !
>>> my_bytes[0] = 3 Traceback (most recent call last): File "", line 1, in my_bytes[0] = 3 TypeError: 'bytes' object does not support item assignment
To fix this error, don’t change the bytes type in your code snippet or use a mutable bytearray instead of an immutable bytes type.
Where to Go From Here
Thanks for reading through the whole tutorial—I’m happy and grateful that you visited us on the Finxter blog because my mission is to help coders like you reach their goals faster.
I believe in the transformative power we coders have to change the world and make it more efficient!
If you want to keep improving your skills, feel free to check out my free email academy and download Python Cheat Sheets here:
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.
Be on the Right Side of Change 🚀
- The world is changing exponentially. Disruptive technologies such as AI, crypto, and automation eliminate entire industries. 🤖
- Do you feel uncertain and afraid of being replaced by machines, leaving you without money, purpose, or value? Fear not! There a way to not merely survive but thrive in this new world!
- Finxter is here to help you stay ahead of the curve, so you can keep winning as paradigms shift.
Learning Resources 🧑💻
⭐ Boost your skills. Join our free email academy with daily emails teaching exponential with 1000+ tutorials on AI, data science, Python, freelancing, and Blockchain development!
Join the Finxter Academy and unlock access to premium courses 👑 to certify your skills in exponential technologies and programming.
New Finxter Tutorials:
Finxter Categories: