hexadecimal to strings using python

Question:

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

Asked By: SEUN

||

Answers:

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']
Answered By: Tim Roberts
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.