Hexadecimal String to Byte Array in Python

hexadecimal string to byte array in python

Suppose your hex string is something like

>>> hex_string = "deadbeef"

Convert it to a bytearray (Python 3 and 2.7):

>>> bytearray.fromhex(hex_string)
bytearray(b'\xde\xad\xbe\xef')

Convert it to a bytes object (Python 3):

>>> bytes.fromhex(hex_string)
b'\xde\xad\xbe\xef'

Note that bytes is an immutable version of bytearray.

Convert it to a string (Python ≤ 2.7):

>>> hex_data = hex_string.decode("hex")
>>> hex_data
"\xde\xad\xbe\xef"

Converting and Printing Hex string to Byte

The first line: bytes_object = bytes.fromhex(Str0) gives you a bytes type that functions as raw bytes data / byte array.
to print the numeric value of each byte, what you need to do is:
for x in bytes_object : print(x)
If you want an array of the numeric values you can use [int(x) for x in bytes_object]

Byte Array to Hex String

Using str.format:

>>> array_alpha = [ 133, 53, 234, 241 ]
>>> print ''.join('{:02x}'.format(x) for x in array_alpha)
8535eaf1

or using format

>>> print ''.join(format(x, '02x') for x in array_alpha)
8535eaf1

Note: In the format statements, the 02 means it will pad with up to 2 leading 0s if necessary. This is important since [0x1, 0x1, 0x1] i.e. (0x010101) would be formatted to "111" instead of "010101"

or using bytearray with binascii.hexlify:

>>> import binascii
>>> binascii.hexlify(bytearray(array_alpha))
'8535eaf1'

Here is a benchmark of above methods in Python 3.6.1:

from timeit import timeit
import binascii

number = 10000

def using_str_format() -> str:
return "".join("{:02x}".format(x) for x in test_obj)

def using_format() -> str:
return "".join(format(x, "02x") for x in test_obj)

def using_hexlify() -> str:
return binascii.hexlify(bytearray(test_obj)).decode('ascii')

def do_test():
print("Testing with {}-byte {}:".format(len(test_obj), test_obj.__class__.__name__))
if using_str_format() != using_format() != using_hexlify():
raise RuntimeError("Results are not the same")

print("Using str.format -> " + str(timeit(using_str_format, number=number)))
print("Using format -> " + str(timeit(using_format, number=number)))
print("Using binascii.hexlify -> " + str(timeit(using_hexlify, number=number)))

test_obj = bytes([i for i in range(255)])
do_test()

test_obj = bytearray([i for i in range(255)])
do_test()

Result:

Testing with 255-byte bytes:
Using str.format -> 1.459474583090427
Using format -> 1.5809937679100738
Using binascii.hexlify -> 0.014521426401399307
Testing with 255-byte bytearray:
Using str.format -> 1.443447684109402
Using format -> 1.5608712609513171
Using binascii.hexlify -> 0.014114164661833684

Methods using format do provide additional formatting options, as example separating numbers with spaces " ".join, commas ", ".join, upper-case printing "{:02X}".format(x)/format(x, "02X"), etc., but at a cost of great performance impact.

How to convert hex string to bytes

You have spaces in your argument.

from binascii import unhexlify    
data = unhexlify(b'539B333B39706D149028CFE1D9D4A407')
print(data) # --> b'S\x9b3;9pm\x14\x90(\xcf\xe1\xd9\xd4\xa4\x07'

How to create python bytes object from long hex string?

result = bytes.fromhex(some_hex_string)

I want to convert a hexadecimal byte array to hexadecimal in string format

def format_bytes_as_hex(b: bytes) -> str:
h = b.hex()
return ' ' .join(f'{a}{b}'.upper() for a, b in zip(h[0::2], h[1::2]))

Test:
format_bytes_as_hex(b'\xfc\x81\xe4\xa2\xb9\x92')
'FC 81 E4 A2 B9 92'

How to convert list of hex values into a byte array in python

That can be solved by a simple one line expression

input = ['0x1', '0x3', '0x2', '0x0', '0x0', '0x10', '0x4', '0x0', '0x0', '0xfa', '0x4']
result = bytes([int(x,0) for x in input])

The result is

b'\x01\x03\x02\x00\x00\x10\x04\x00\x00\xfa\x04'

If you do not actually want to have a byte array, but an array of integers, just remove the bytes()

result = [int(x,0) for x in input]

The result is

[1, 3, 2, 0, 0, 16, 4, 0, 0, 250, 4]    

What's the correct way to convert bytes to a hex string in Python 3?

Since Python 3.5 this is finally no longer awkward:

>>> b'\xde\xad\xbe\xef'.hex()
'deadbeef'

and reverse:

>>> bytes.fromhex('deadbeef')
b'\xde\xad\xbe\xef'

works also with the mutable bytearray type.

Reference: https://docs.python.org/3/library/stdtypes.html#bytes.hex



Related Topics



Leave a reply



Submit