2

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

0

1 Answer 1

2

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']
Sign up to request clarification or add additional context in comments.

4 Comments

bytes.fromhex probably more usual, since we don't need a mutable bytearray here.
Good point. My Google search came up with the bytesarray example, so I just copied it.
@TimRoberts can you please check to see why i get error (UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf9 in position 2: invalid start byte)
That says you are trying to translate strings that are not in UTF-8. You can remove the .decode() to see the 8-bit version. If you know what character you expected it to be, you can figure out what code page you're using and how to do the decode.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.