I want to get a list of ints representing the bytes in a string.
asked Jul 15, 2010 at 13:35
Juanjo ContiJuanjo Conti
27.5k39 gold badges106 silver badges131 bronze badges
8
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]
answered Jul 15, 2010 at 13:40
Scott GriffithsScott Griffiths
20.9k8 gold badges54 silver badges84 bronze badges
1
Do you mean the ascii values?
nums = [ord[c] for c in mystring]
or
nums = []
for chr in mystring:
nums.append[ord[chr]]
answered Jul 15, 2010 at 13:38
Donald MinerDonald Miner
37.7k8 gold badges90 silver badges117 bronze badges
2
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.
See: //docs.python.org/library/struct.html
answered Jul 15, 2010 at 13:59
4
Python 3.2 now has a function called int.from_bytes[] to convert bytes to an integer. To create bytes in Python, use the bytes[] method. The bytes[] is a built-in method that returns immutable bytes object initialized with the given size and data.
To convert bytes to int in Python, use the int.from_bytes[] method. A byte value can be interchanged to an int value using the int.from_bytes[] function.
The int.from_bytes[] function takes bytes, byteorder, signed, * as parameters and returns the integer represented by the given array of bytes.
Syntax
int.from_bytes[bytes, byteorder, *, signed=False]
Arguments
bytes: It is a byte object.
byteorder: It determines the order of representation of the integer value. The byteorder can have values as either “little,” where the most significant bit is stored at the end of “big”, where MSB is stored at the start, and LSB at the end.
signed: It has a False default value. It indicates whether to represent 2’s complement of a number.
Return Value
It returns the integer represented by the given array of bytes.
Example
# Declaring byte value byte_val = b'\x21\x19' # Converting to int int_val = int.from_bytes[byte_val, "big"] # printing int equivalent print[int_val]
Output
8473
You can see that we passed byteorder = big. The byteorder argument determines the byte order used to represent the integer. If byteorder is “little“, the most significant byte is at the beginning of the byte array.
Passing byteorder = “little”
If byteorder is “little“, the most significant byte is at the end of the byte array.
# Declaring byte value byte_val = b'\x11\x21' # Converting to int int_val = int.from_bytes[byte_val, "little"] # printing int equivalent print[int_val]
Output
8465
Passing signed=True
The int.from_bytes[] method also accepts the signed argument. By default, its value is False.
Let’s another example and pass signed = True and see the output.
# Declaring byte value byte_val = b'\xfc\x00' # Converting to int int_val = int.from_bytes[byte_val, "big", signed=True] # printing int equivalent print[int_val]
Output
-1024
I hope you found the answer you are looking for, and That is it for converting bytes to integer in Python example.
See also
Python string to int
Python string to hex
Python string to dictionary
Python string to json
Python string to array