+--------+-------------------------+--------------------+-------+
| ``i`` | :ctype:`int` | integer | |
+--------+-------------------------+--------------------+-------+
-| ``I`` | :ctype:`unsigned int` | long | |
+| ``I`` | :ctype:`unsigned int` | integer or long | |
+--------+-------------------------+--------------------+-------+
| ``l`` | :ctype:`long` | integer | |
+--------+-------------------------+--------------------+-------+
+--------+-------------------------+--------------------+-------+
| ``p`` | :ctype:`char[]` | string | |
+--------+-------------------------+--------------------+-------+
-| ``P`` | :ctype:`void \*` | integer | |
+| ``P`` | :ctype:`void \*` | long | |
+--------+-------------------------+--------------------+-------+
Notes:
The :mod:`struct` module provides :func:`pack` and :func:`unpack` functions for
working with variable length binary record formats. The following example shows
-how to loop through header information in a ZIP file (with pack codes ``"H"``
-and ``"L"`` representing two and four byte unsigned numbers respectively)::
+how to loop through header information in a ZIP file without using the
+:mod:`zipfile` module. Pack codes ``"H"`` and ``"I"`` represent two and four
+byte unsigned numbers respectively. The ``"<"`` indicates that they are
+standard size and in little-endian byte order::
import struct
start = 0
for i in range(3): # show the first 3 file headers
start += 14
- fields = struct.unpack('LLLHH', data[start:start+16])
+ fields = struct.unpack('<IIIHH', data[start:start+16])
crc32, comp_size, uncomp_size, filenamesize, extra_size = fields
start += 16
#----------------------------------------------------------------------
from test import test_support
-import os, struct, stat, sys
+import os, stat, sys
try:
import signal