I want to get a list of ints representing the bytes in a string.
3 Answers
One option for Python 2.6 and later is to use a bytearray:
>>> b = bytearray('hello')
>>> b[0]
104
>>> b[1]
101
>>> list(b)
[104, 101, 108, 108, 111]
For Python 3.x you'd need a bytes object rather than a string in any case and so could just do this:
>>> b = b'hello'
>>> list(b)
[104, 101, 108, 108, 111]
1 Comment
Justin Ardini
To clarify for the OP, these values are the ascii values.
Perhaps you mean a string of bytes, for example received over the net, representing a couple of integer values?
In that case you can "unpack" the string into the integer values by using unpack() and specifying "i" for integer as the format string.
"00010100"to 20 and so on - who wanna bet on that ;-)