![]() |
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 BytesBelow, are the ways to Python Convert Hex String To Bytes in Python.
Convert Hex String To Bytes Using
|
hex_string = "1a2b3c" bytes_result = bytes.fromhex(hex_string) print ( type (hex_string)) print (bytes_result) print ( type (bytes_result)) |
<class 'str'> b'\x1a+<' <class 'bytes'>
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.
hex_string = "1a2b3c" bytearray_result = bytearray.fromhex(hex_string) print ( type (hex_string)) print (bytearray_result) print ( type (bytearray_result)) |
<class 'str'> bytearray(b'\x1a+<') <class 'bytearray'>
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.
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)) |
<class 'str'> b'\x1a+<' <class 'bytes'>
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.
import binascii hex_string = "1a2b3c" bytes_result = binascii.unhexlify(hex_string) print ( type (hex_string)) print (bytes_result) print ( type (bytes_result)) |
<class 'str'> b'\x1a+<' <class 'bytes'>
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.
Reffered: https://www.geeksforgeeks.org
Geeks Premier League |
Related |
---|
![]() |
![]() |
![]() |
![]() |
|
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |