Convert string to ASCII value python

PythonAscii

Python Problem Overview


How would you convert a string to ASCII values?

For example, "hi" would return [104 105].

I can individually do ord('h') and ord('i'), but it's going to be troublesome when there are a lot of letters.

Python Solutions


Solution 1 - Python

You can use a list comprehension:

>>> s = 'hi'
>>> [ord(c) for c in s]
[104, 105]

Solution 2 - Python

Here is a pretty concise way to perform the concatenation:

>>> s = "hello world"
>>> ''.join(str(ord(c)) for c in s)
'10410110810811132119111114108100'

And a sort of fun alternative:

>>> '%d'*len(s) % tuple(map(ord, s))
'10410110810811132119111114108100'

Solution 3 - Python

If you are using python 3 or above,

>>> list(bytes(b'test'))
[116, 101, 115, 116]

Solution 4 - Python

If you want your result concatenated, as you show in your question, you could try something like:

>>> reduce(lambda x, y: str(x)+str(y), map(ord,"hello world"))
'10410110810811132119111114108100'

Solution 5 - Python

In 2021 we can assume only Python 3 is relevant, so...

If your input is bytes:

>>> list(b"Hello")
[72, 101, 108, 108, 111]

If your input is str:

>>> list("Hello".encode('ascii'))
[72, 101, 108, 108, 111]

If you want a single solution that works with both:

list(bytes(text, 'ascii'))

(all the above will intentionally raise UnicodeEncodeError if str contains non-ASCII chars. A fair assumption as it makes no sense to ask for the "ASCII value" of non-ASCII chars.)

Solution 6 - Python

your description is rather confusing; directly concatenating the decimal values doesn't seem useful in most contexts. the following code will cast each letter to an 8-bit character, and THEN concatenate. this is how standard ASCII encoding works

def ASCII(s):
    x = 0
    for i in xrange(len(s)):
	    x += ord(s[i])*2**(8 * (len(s) - i - 1))
    return x

Solution 7 - Python

It is not at all obvious why one would want to concatenate the (decimal) "ascii values". What is certain is that concatenating them without leading zeroes (or some other padding or a delimiter) is useless -- nothing can be reliably recovered from such an output.

>>> tests = ["hi", "Hi", "HI", '\x0A\x29\x00\x05']
>>> ["".join("%d" % ord(c) for c in s) for s in tests]
['104105', '72105', '7273', '104105']

Note that the first 3 outputs are of different length. Note that the fourth result is the same as the first.

>>> ["".join("%03d" % ord(c) for c in s) for s in tests]
['104105', '072105', '072073', '010041000005']
>>> [" ".join("%d" % ord(c) for c in s) for s in tests]
['104 105', '72 105', '72 73', '10 41 0 5']
>>> ["".join("%02x" % ord(c) for c in s) for s in tests]
['6869', '4869', '4849', '0a290005']
>>>

Note no such problems.

Solution 8 - Python

def stringToNumbers(ord(message)):
    return stringToNumbers
    stringToNumbers.append = (ord[0])
    stringToNumbers = ("morocco")

Solution 9 - Python

you can actually do it with numpy:

import numpy as np
a = np.fromstring('hi', dtype=np.uint8)
print(a)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNeal WangView Question on Stackoverflow
Solution 1 - PythonMark ByersView Answer on Stackoverflow
Solution 2 - PythonAndrew ClarkView Answer on Stackoverflow
Solution 3 - PythondevuntView Answer on Stackoverflow
Solution 4 - PythonNateView Answer on Stackoverflow
Solution 5 - PythonMestreLionView Answer on Stackoverflow
Solution 6 - PythonJason SteinView Answer on Stackoverflow
Solution 7 - PythonJohn MachinView Answer on Stackoverflow
Solution 8 - PythonislamView Answer on Stackoverflow
Solution 9 - PythonJ.R.View Answer on Stackoverflow