示例工程 31-io-files-struct-socket-sqlite

31-io-files-struct-socket-sqlite/03-struct-binary-header/README.md

markdown · 0.6 KB

# 03 · struct-binary-header
 
**演示**:`struct.pack("<4sHIH", ...)` 生成 12 字节小端头(magic 4B + uint16 + uint32 + uint16),`unpack`/`calcsize` 验证布局与字节序。
**运行**:
 
```sh
uv run python examples/31-io-files-struct-socket-sqlite/03-struct-binary-header/struct_binary_header.py
```
 
**预期输出**(3.12.9):
 
```text
size: 12
hex: 585752540100000400000300
decoded: (b'XWRT', 1, 1024, 3)
calcsize: 12
```
 
**依赖**:仅标准库。
**注意**:`<` 是小端、`>` 是大端;网络协议通常用 `!`(大端)。不带前缀时使用本机对齐,跨平台会变。