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']
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']
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']
L = ['h','e','l','l','o']
print(list(map(str, map(ord, L))))
This outputs:
['104', '101', '108', '108', '111']
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]
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()
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
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]
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']
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']
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']
L = ['h','e','l','l','o']
print(list(map(str, map(ord, L))))
This outputs:
['104', '101', '108', '108', '111']
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]
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()
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
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]