1"""
2Python 3
3"""
4
5# int.from_bytes( bytes, byteorder, *, signed=False )
6# bytes must evaluate as bytes-like object
7# byteorder is optional. someString must evaluate as 'big' xor 'little'
8# signed is optional. someBoolean opmust evaluate as boolean
9
10
11# examples:
12someBytes = b'\x00\x01\x00\x02\x00\x03'
13someInteger = int.from_bytes(someBytes)
14someInteger = int.from_bytes(someBytes, 'big')
15someInteger = int.from_bytes(someBytes, byteorder='little', signed=True)
16
17
18"""
19Python 2.7
20"""
21
22# no bytes is str in Python 2.7
23
24import struct
25struct.unpack(pattern, someBytes)
26
27# someBytes is of type str or byte array
28
29# pattern is a string begining with '>' or '<',
30# followed by as many 'H' or 'B' chars needed
31# to cover the someBytes' length.
32
33# '>' stands for Big-endian
34# '<' stands for Big-endian
35# 'H' stands for 2 bytes unsigned short integer
36# 'B' stands for 1 byte unsigned char
37
38
39# examples:
40
41>>> import struct
42>>> sixBytes = b'\x00\x01\x00\x02\x00\x03'
43>>> struct.unpack('>HHH', sixBytes)
44(1, 2, 3)
45
46>>> struct.unpack('<HHH', sixBytes)
47(256, 512, 768)
48
49>>> struct.unpack('>BBBBBB', sixBytes)
50(0, 1, 0, 2, 0, 3)
51
52>>> struct.unpack('<BBBBBB', sixBytes)
53(0, 1, 0, 2, 0, 3)