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
How many kilobytes is 1000 bytes?
1000 bytes are exactly 1 kilobyte. The kilobyte is a decimal multiple of the byte: don't mistake it with the kibibyte, the binary multiple, that corresponds to 1024 bytes. Kilobyte and kibibyte differ only by 2.4% in size, but this difference grows exponentially with the orders of magnitude of memory, causing many difficulties and misunderstandings when talking about computer memories!
What is a byte?
A byte is the most common unit of digital information used in modern computing. A byte corresponds to 8 bits and is capable of storing 256 different combinations. Bytes are the basis of digital computing: even though your computer works with bits, you can't manipulate anything smaller than a byte. By the way, every single character in this answer is encoded by a byte, as everything else on your device!
What is the difference between bit and byte?
A bit is the basic unit of digital information, capable of storing a single binary value, 0 or 1. Think of a bit as an object with only two possible states, on and off (or true and false). By combining bits, you can create increasingly complex combinations of those states. Two bits store four possible combinations: 00, 01, 10, and 11. This number grows, and with 8 bits, you can create 256 words: this is enough to store letters, numbers, and symbols, leaving room to spare, and scientists eventually adopted this as standard.