python - Byte Array to Hex String -


i have data stored in byte array. how can convert data hex string?

example of byte array:

array_alpha = [ 133, 53, 234, 241 ] 

using str.format:

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

or using format

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

note: in format statements, 02 means pad 2 leading 0s if necessary. important since [0x1, 0x1, 0x1] i.e. (0x010101) formatted "111" instead of "010101"

or using bytearray binascii.hexlify:

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

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -