Struct
The `struct` module packs and unpacks binary data as Python bytes objects. It is essential for working with binary file formats, network protocols, and low-level data manipulation. The guide walks through Packing & Unpacking, Byte Order & Alignment, Working with Binary Files, struct & Network Protocols, Advanced Patterns. `struct.pack(format, v1, v2, ...)` converts values to bytes according to the format string. `struct.unpack(format, data)` converts bytes back to a tuple of values. Common format characters: `i` (int), `f` (float), `d` (double), `s` (bytes), `c` (char), `b`/`B` (signed/unsigned byte), `h`/`H` (short), `l`/`L` (long), `q`/`Q` (long long). Example: `struct.pack("i3sf", 1, b"abc", 2.5)` creates an 12-byte binary packet. Format strings start with a byte order prefix: ` ` (big-endian), `!` (network/big-endian), `=` (native), `@` (native with alignment). Little-endian: Intel/AMD x86, most ARM. Big-endian: network protocols, some file formats. Native order matches your CPU. Network order (`!`) is standard for data interchange. Example: `struct.pack("!HHL", 1, 2, 3)` packs as big-endian. Read binary files with `open("file.bin", "rb")` and `file.read(size)`. Parse structured data by unpacking chunks. Example: reading a 24-byte header with 4 fields: `data = f.read(24); (version, flags, size, checksum) = struct.unpack("