Assuming you're on at least 3.2, there's a built in for this:
int.from_bytes(bytes,byteorder, *,signed=False)...
The argument
bytesmust either be a bytes-like object or an iterable producing bytes.The
byteorderargument determines the byte order used to represent the integer. Ifbyteorderis"big", the most significant byte is at the beginning of the byte array. Ifbyteorderis"little", the most significant byte is at the end of the byte array. To request the native byte order of the host system, usesys.byteorderas the byte order value.The
signedargument indicates whether two’s complement is used to represent the integer.
## Examples:
int.from_bytes(b'\x00\x01', "big") # 1
int.from_bytes(b'\x00\x01', "little") # 256
int.from_bytes(b'\x00\x10', byteorder='little') # 4096
int.from_bytes(b'\xfc\x00', byteorder='big', signed=True) #-1024
Answer from Peter DeGlopper on Stack OverflowAssuming you're on at least 3.2, there's a built in for this:
int.from_bytes(bytes,byteorder, *,signed=False)...
The argument
bytesmust either be a bytes-like object or an iterable producing bytes.The
byteorderargument determines the byte order used to represent the integer. Ifbyteorderis"big", the most significant byte is at the beginning of the byte array. Ifbyteorderis"little", the most significant byte is at the end of the byte array. To request the native byte order of the host system, usesys.byteorderas the byte order value.The
signedargument indicates whether two’s complement is used to represent the integer.
## Examples:
int.from_bytes(b'\x00\x01', "big") # 1
int.from_bytes(b'\x00\x01', "little") # 256
int.from_bytes(b'\x00\x10', byteorder='little') # 4096
int.from_bytes(b'\xfc\x00', byteorder='big', signed=True) #-1024
Lists of bytes are subscriptable (at least in Python 3.6). This way you can retrieve the decimal value of each byte individually.
>>> intlist = [64, 4, 26, 163, 255]
>>> bytelist = bytes(intlist) # b'@\x04\x1a\xa3\xff'
>>> for b in bytelist:
... print(b) # 64 4 26 163 255
>>> [b for b in bytelist] # [64, 4, 26, 163, 255]
>>> bytelist[2] # 26
python - Why does "bytes(n)" create a length n byte string instead of converting n to a binary representation? - Stack Overflow
python - How to convert a string of bytes into an int? - Stack Overflow
Converting integer to byte string problem in python 3
Byte to integer ? why so complicated in python?
From python 3.2 you can use to_bytes:
>>> (1024).to_bytes(2, byteorder='big')
b'\x04\x00'
def int_to_bytes(x: int) -> bytes:
return x.to_bytes((x.bit_length() + 7) // 8, 'big')
def int_from_bytes(xbytes: bytes) -> int:
return int.from_bytes(xbytes, 'big')
Accordingly, x == int_from_bytes(int_to_bytes(x)).
Note that the above encoding works only for unsigned (non-negative) integers.
For signed integers, the bit length is a bit more tricky to calculate:
def int_to_bytes(number: int) -> bytes:
return number.to_bytes(length=(8 + (number + (number < 0)).bit_length()) // 8, byteorder='big', signed=True)
def int_from_bytes(binary_data: bytes) -> Optional[int]:
return int.from_bytes(binary_data, byteorder='big', signed=True)
That's the way it was designed - and it makes sense because usually, you would call bytes on an iterable instead of a single integer:
>>> bytes([3])
b'\x03'
The docs state this, as well as the docstring for bytes:
>>> help(bytes)
...
bytes(int) -> bytes object of size given by the parameter initialized with null bytes
In Python 3.2 and later, use
>>> int.from_bytes(b'y\xcc\xa6\xbb', byteorder='big')
2043455163
or
>>> int.from_bytes(b'y\xcc\xa6\xbb', byteorder='little')
3148270713
according to the endianness of your byte-string.
This also works for bytestring-integers of arbitrary length, and for two's-complement signed integers by specifying signed=True. See the docs for from_bytes.
You can also use the struct module to do this:
>>> struct.unpack("<L", "y\xcc\xa6\xbb")[0]
3148270713L
Hi, I have a peculiar setup. I have a byte array converted to a string. It resembles something like: bytearray(b'\xfa\xff\xff\xff\xff\xff\xff....
The string arrives from a TCP socket, and I want to convert this string into a list or numpy array of integers from 0 to 255.
I am able to convert to a list resembling this: 'ff', 'ff', 'f9', 'ff', 'ff', 'ff', '00', '00', '00', '00', 'f6', 'ff', 'ff', 'ff', '00', '00', '00',...
However, using decode('utf-8') did not seem to work.
I have tried several methods with limited outcomes. For example, for loops and lambdas to iterate through each loop, trying to use int(), etc.
Since this is python 3, I get errors stating that 'Str' and list have no attribute 'decode.'
Any suggestions on making this more efficient? Some Codec function perhaps?