Prev: marshal Up: Built-in Modules Top: Top
struct
The module defines the following exception and functions:
v1, v2, ...
packed according to the given
format. The arguments must match the values required by the format
exactly.
pack(fmt, ... )
)
according to the given format. The result is a tuple even if it
contains exactly one item. The string must contain exactly the
amount of data required by the format (i.e. len(string)
must
equal calcsize(fmt)
).
'4h'
means exactly the same as 'hhhh'
.
C numbers are represented in the machine's native format and byte order, and properly aligned by skipping pad bytes if necessary (according to the rules used by the C compiler).
Examples (all on a big-endian machine):
pack('hhl', 1, 2, 3) == '\000\001\000\002\000\000\000\003'
unpack('hhl', '\000\001\000\002\000\000\000\003') == (1, 2, 3)
calcsize('hhl') == 8
'llh0l'
specifies two
pad bytes at the end, assuming longs are aligned on 4-byte boundaries.
(More format characters are planned, e.g. 's'
for character
arrays, upper case for unsigned variants, and a way to specify the
byte order, which is useful for [de]constructing network packets and
reading/writing portable binary file formats like TIFF and AIFF.)