How to convert a list into ASCII

Question:

How can I convert a list into ASCII, but I want to have a list again after I converted it.

I found this for converting from ASCII to a list:

L = [104, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100]
print(''.join(map(chr,L)))

But it doesn’t work reversed.

This is my input:

L = ['h','e','l','l','o']

And I want this as output:

L = ['104','101','108','108','111']
Asked By: BOB

||

Answers:

This worked well for me.

>>> L = [104, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100]
>>> print [ chr(x) for x in L]
['h', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd']
Answered By: ItayBenHaim
L = ['h','e','l','l','o']

print(list(map(ord,L)))
#output : [104, 101, 108, 108, 111]

print(list(map(str,map(ord,L))))
#output : ['104', '101', '108', '108', '111']
Answered By: Hearner
L = ['h','e','l','l','o']
print(list(map(str, map(ord, L))))

This outputs:

['104', '101', '108', '108', '111']
Answered By: blhsing

This works the other way round:

L = ['h', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd']
L = [ord(x) for x in L]
print(L)

output:

[104, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100]
Answered By: Jones1220

This is a much simpler solution:

L = ['h','e','l','l','o']
changer = [ord(x) for x in L]
print(changer)

Using the function ord()

Answered By: Mahir Islam

From here

You can try:

L = ['h','e','l','l','o']
for x in range(len(L)):
     L[x] = ord(L[x])
print(L)

Ouput:

[104,101,108,108,111]

EDIT:

ord() allows you to convert from char to ASCII whereas chr() allows you to do the opposite

Answered By: Vex –
message = list(input("Message: "))
message = [ord(x) for x in message]
print(message)

Here is my solution for this. Hope this helps! Output depends on your input, but in general it will follow the form

[x, y, z]
Answered By: Renegade3301
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.