I want to convert a list of bytes as a string of hex values. The list of bytes is a bit long, so I want spaces between the bytes to improve readability. The function hexlify
as well as hex
does the job:
import binascii
a = [1,2,3,4]
s = binascii.hexlify(bytearray(a))
print s
s = bytes(a).hex()
print s
But the result is '01020304'. I want a dump with a space between the bytes like '01 02 03 04'. How can I do this in an efficient way?
Edit: There is also a way to iterate all bytes. Would this be efficient?
s = ' '.join('%02x' % i for i in a)