Say that I have a 4 character string, and I want to convert this string into a byte array where each character in the string is translated into its hex equivalent. e.g.
This work in both Python 2 and 3:
>>> bytearray(b'ABCD') bytearray(b'ABCD')
Note string started with b.
b
To get individual chars:
>>> print("DEC HEX ASC") ... for b in bytearray(b'ABCD'): ... print(b, hex(b), chr(b)) DEC HEX ASC 65 0x41 A 66 0x42 B 67 0x43 C 68 0x44 D
Hope this helps