How to Convert Bytes to Integers

Bytes data type has the value ranging from 0 to 255 (0x00 to 0xFF). One byte has 8 bits, that’s why its maximum value is 0xFF. In some circumstances, you need to converter bytes or bytes array to integers for further data processing. This short article introduces you how to do the bytes to integers conversion.

Python 2.7 Bytes

There is no built-in bytes data type in Python 2.7 version. Keyword byte is identical to str.

>>> bytes is str
True

bytearray is used to define a bytes or byte array object.

>>> byteExample1 = bytearray([1])
>>> byteExample1
bytearray(b'\x01')
>>> byteExample2 = bytearray([1,2,3])
>>> byteExample2
bytearray(b'\x01\x02\x03')

Convert Bytes to Integers in Python 2.7

Python internal module struct could convert binary data (bytes) to integers. It could convert bytes or actually strings in Python 2.7 and integers in a bidirectional way.

struct.unpack(fmt, string)
	Convert the string according to the given format `fmt` to integers. The result is a tuple even if there is only one item inside.

struct Examples

import struct
testBytes = b'\x00\x01\x00\x02'
testResult = struct.unpack('>HH', testBytes)
print testResult
(1, 2)

The format string >HH contains two parts. 1. > indicates the binary data is big-endian, or in other words, the data is ordered from big end (most significant bit). For example, \x00\0x1 means \x00 is high byte and \x01 is low byte. 2. HH means there are two objects of H types in the bytes string. H represents unsigned short integer which takes 2 bytes.

You could get different results from the same string if the assigned data format is different.

>>> testResult = struct.unpack('<HH', testBytes)
>>> testResult
(256, 512)

Here, < indicates the endianess is little-endian. Therefore \x00\x01 becomes 00+1*256 = 256, not 0*256+1 = 1 anymore.

>>> testResult = struct.unpack('<BBBB', testBytes)
>>> testResult
(0, 1, 0, 2)

B means the data is unsigned char taking 1 byte. Hence, \x00\x01\x00\x02 will be converted to 4 values of unsigned char, not 2 values of unsigned short anymore.

Warning
The data length represented by the format string shall be the same with the given data, otherwise, it reports an error
>>> testResult = struct.unpack('<BBB', b'\x00\x01\x00\x02')

Traceback (most recent call last):
  File "<pyshell#35>", line 1, in <module>
    testResult = struct.unpack('<BBB', b'\x00\x01\x00\x02')
error: unpack requires a string argument of length 3

You could check official document of struct module to get more information of format strings.

Python 3 Bytes

bytes is a built-in data type in Python 3, therefore, you could define bytes directly using bytes keyword.

>>> testByte = bytes(18)
>>> type(testByte)

You could also directly define a bytes or bytes array like below

>>> testBytes = b'\x01\x21\31\41'
>>> type(testBytes)

Convert Bytes to Integers in Python 3

Besides struct module as already introduced in Python 2.7, you could also use new Python 3 built-in integer method to do the bytes-to-integers conversions, that is, int.from_bytes() method.

int.from_bytes() Examples

>>> testBytes = b'\xF1\x10'
>>> int.from_bytes(testBytes, byteorder='big')
61712

The byteorder option is similar to struct.unpack() format byte order definition.

Info
The bytes representation will be converted to one integer

int.from_bytes() has a third option signed to assign the integer type to be signed or unsigned.

>>> testBytes = b'\xF1\x10'
>>> int.from_bytes(testBytes, byteorder='big', signed=True)
-3824

Use [] When Bytes is unsinged chart

If the format of data has the format of unsigned chart that contains only one byte, you could directly use object index to access and also get the integer of the data.

>>> testBytes = b'\xF1\x10'
>>> testBytes[0]
241
>>> testBytes[1]
16