How to convert bytes to string to integers? python

I want to get an int list representing bytes in a string.

+3
source share
3 answers

One option for Python 2.6 and later is to use bytearray:

>>> b = bytearray('hello')
>>> b[0]
104
>>> b[1]
101
>>> list(b)
[104, 101, 108, 108, 111]

For Python 3.x, you need bytesan object, not a string anyway, and therefore can just do it

>>> b = b'hello'
>>> list(b)
[104, 101, 108, 108, 111]
+7
source

Do you mean ascii values?

nums = [ord(c) for c in mystring]

or

nums = []
for chr in mystring:
    nums.append(ord(chr))
+5
source

, , , , ?

"" , unpack() "i" .

: http://docs.python.org/library/struct.html

+2

Source: https://habr.com/ru/post/1754813/


All Articles