How to Use Struct
The struct module includes functions for converting between strings of bytes and native Python data types such as numbers and strings. It is used mostly for handling binary data stored in files or from network connections, among other sources.
import struct
pack() & unpack()
- h: short
- l: long
- i: int
- ?: bool
- q: long long int
- f: float
- b: binary
struct.pack takes non-bytes values and converts them into bytes. In contrast, struct.unpack takes bytes and converts them into higher-order equivalents.
var = struct.pack('hli?qf', 1, 2, 3, True, 5, 6.7)
print(var)
tup = struct.unpack('hli?qf', var)
print(tup)
>>> var = struct.pack('hli?qf', 1, 2, 3, True, 5, 6.7)
>>> print(var)
b'\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x01\x00\x00\x00\x05\x00\x00\x00\x00\x00\x00\x00ff\xd6@'
>>> tup = struct.unpack('hli?qf', var)
>>> print(tup)
(1, 2, 3, True, 5, 6.699999809265137)
Endianness
>: little-endian
<: big-endian
=: standard
!: network
@: native
>>> var = struct.pack('hhl', 1, 2, 3)
>>> print(var)
b'\x01\x00\x02\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00'
>>> var = struct.pack('>hhl', 1, 2, 3)
>>> print(var)
b'\x00\x01\x00\x02\x00\x00\x00\x03'
>>> var = struct.pack('<hhl', 1, 2, 3)
>>> print(var)
b'\x01\x00\x02\x00\x03\x00\x00\x00'
>>> var = struct.pack('=hhl', 1, 2, 3)
>>> print(var)
b'\x01\x00\x02\x00\x03\x00\x00\x00'
>>> var = struct.pack('!hhl', 1, 2, 3)
>>> print(var)
b'\x00\x01\x00\x02\x00\x00\x00\x03'
>>> var = struct.pack('@hhl', 1, 2, 3)
>>> print(var)
b'\x01\x00\x02\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00'
calcsize()
Use struct.calcsize() to get the size of the struct. It is required for other functions like struct.pack_into() and struct.unpack_from().
>>> print(struct.calcsize('i'))
4
>>> print(struct.calcsize('l'))
8
>>> print(struct.calcsize('b'))
1
>>> print(struct.calcsize('h'))
2
>>> print(struct.calcsize('?'))
1
>>> print(struct.calcsize('q'))
8
>>> print(struct.calcsize('f'))
4
>>> var = struct.pack('bi', 65, 1)
>>> print(var)
b'A\x00\x00\x00\x01\x00\x00\x00'
>>> print(struct.calcsize('bi'))
8
>>> var = struct.pack('ib', 1, 65)
>>> print(var)
b'\x01\x00\x00\x00A'
>>> print(struct.calcsize('ib'))
5
ref: