Reputation: 23
i am new to python and trying to convert a list of hexadecimal to string
chunk= [' ho', 'w a', 're ', 'you']
chunk_2 = [i.encode('utf-8').hex() for i in chunk]
print(chunk_2)
['20686f', '772061', '726520', '796f75']
chunk_3 = [int(i, base=16) for i in chunk_2]
print(chunk_3)
[2123887, 7807073, 7496992, 7958389]
(convert chunk_3 to hexadecimal)
chunk_4 = [f'{i:x}' for i in chunk_3]
print (chunk_4)
['20686f', '772061', '726520', '796f75']
how can i convert hexadecimal chunk_4 back to list of string inchunk
Upvotes: 2
Views: 75
Reputation: 54787
You can use bytearray for this.
>>> chunk_4 = ['20686f', '772061', '726520', '796f75']
>>> [bytes.fromhex(k).decode() for k in chunk_4]
[' ho', 'w a', 're ', 'you']
Upvotes: 2