Python’s treatment of bytes differs significantly between Python 2.7 and Python 3. This article clarifies how to convert bytes to integers in both versions, emphasizing the crucial distinctions.
Table of Contents
- Python 2.7 and Byte Strings
- Converting Bytes to Integers in Python 2.7
- Python 3 and the
bytes
Type - Converting Bytes to Integers in Python 3
Python 2.7 and Byte Strings
In Python 2.7, the str
type handles both text and byte data. A dedicated bytes
type, as found in Python 3, is absent. Byte strings are essentially sequences of characters representing byte values. It’s important to remember that Python 2.7 is officially deprecated and should not be used for new projects.
Converting Bytes to Integers in Python 2.7
Converting single bytes to integers is done with the ord()
function:
byte_string = 'A' # Represents the byte value of 'A'
integer_value = ord(byte_string)
print(integer_value) # Output: 65
ord()
provides the Unicode code point, which matches the byte’s numerical value for single bytes. For longer byte strings, bit manipulation is necessary to convert them to larger integers. The following example assumes little-endian byte order:
byte_string = 'x01x02x03' # Example byte string (little-endian)
integer_value = 0
for byte in byte_string:
integer_value = (integer_value << 8) | ord(byte)
print(integer_value) # Output: 66051
This iteratively shifts the integer left by 8 bits and applies a bitwise OR to incorporate each byte. For big-endian order, reverse the iteration.
Python 3 and the bytes
Type
Python 3 explicitly introduces the bytes
type, separating it from the str
type (Unicode text). bytes
objects are immutable sequences of integers ranging from 0 to 255.
Converting Bytes to Integers in Python 3
Python 3 offers a cleaner approach. For a single byte:
byte_data = b'x41' # Note the 'b' prefix indicating bytes literal
integer_value = byte_data[0]
print(integer_value) # Output: 65
Directly accessing the byte at index 0 yields the integer value. For multiple bytes, int.from_bytes()
is used, specifying the byte order (endianness):
byte_data = b'x01x02x03' # Little-endian example
integer_value = int.from_bytes(byte_data, byteorder='little')
print(integer_value) # Output: 66051
byte_data = b'x01x02x03' # Big-endian example
integer_value = int.from_bytes(byte_data, byteorder='big')
print(integer_value) # Output: 16909060
byteorder='little'
signifies least significant byte first, while byteorder='big'
indicates most significant byte first. Always specify byteorder
for portability; otherwise, the system’s default is used.