blob: 14627b60d6d697ed35912c35cb663a9d6b846528 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{struct} ---
2 Interpret strings as packed binary data.}
Fred Drakeb91e9341998-07-23 17:59:49 +00003\declaremodule{builtin}{struct}
4
5\modulesynopsis{Interpret strings as packed binary data.}
6
Fred Drakeabdea221998-03-16 05:22:08 +00007\indexii{C@\C{}}{structures}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00008
9This module performs conversions between Python values and C
10structs represented as Python strings. It uses \dfn{format strings}
11(explained below) as compact descriptions of the lay-out of the C
12structs and the intended conversion to/from Python values.
13
14The module defines the following exception and functions:
15
Fred Drake7ddd0431998-03-08 07:44:13 +000016
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000017\begin{excdesc}{error}
18 Exception raised on various occasions; argument is a string
19 describing what is wrong.
20\end{excdesc}
21
Fred Drakecce10901998-03-17 06:33:25 +000022\begin{funcdesc}{pack}{fmt, v1, v2, {\rm \ldots}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000023 Return a string containing the values
24 \code{\var{v1}, \var{v2}, {\rm \ldots}} packed according to the given
25 format. The arguments must match the values required by the format
26 exactly.
27\end{funcdesc}
28
Fred Drakecce10901998-03-17 06:33:25 +000029\begin{funcdesc}{unpack}{fmt, string}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000030 Unpack the string (presumably packed by \code{pack(\var{fmt}, {\rm \ldots})})
31 according to the given format. The result is a tuple even if it
32 contains exactly one item. The string must contain exactly the
33 amount of data required by the format (i.e. \code{len(\var{string})} must
34 equal \code{calcsize(\var{fmt})}).
35\end{funcdesc}
36
37\begin{funcdesc}{calcsize}{fmt}
38 Return the size of the struct (and hence of the string)
39 corresponding to the given format.
40\end{funcdesc}
41
42Format characters have the following meaning; the conversion between C
43and Python values should be obvious given their types:
44
Fred Drakeee601911998-04-11 20:53:03 +000045\begin{tableiii}{c|l|l}{samp}{Format}{C Type}{Python}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000046 \lineiii{x}{pad byte}{no value}
47 \lineiii{c}{char}{string of length 1}
48 \lineiii{b}{signed char}{integer}
Guido van Rossum12543461996-12-31 02:22:14 +000049 \lineiii{B}{unsigned char}{integer}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000050 \lineiii{h}{short}{integer}
Guido van Rossum12543461996-12-31 02:22:14 +000051 \lineiii{H}{unsigned short}{integer}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000052 \lineiii{i}{int}{integer}
Guido van Rossum12543461996-12-31 02:22:14 +000053 \lineiii{I}{unsigned int}{integer}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000054 \lineiii{l}{long}{integer}
Guido van Rossum12543461996-12-31 02:22:14 +000055 \lineiii{L}{unsigned long}{integer}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000056 \lineiii{f}{float}{float}
57 \lineiii{d}{double}{float}
Guido van Rossum12543461996-12-31 02:22:14 +000058 \lineiii{s}{char[]}{string}
Fred Drakecf0fb8b1998-07-23 21:18:25 +000059 \lineiii{p}{char[]}{string}
Guido van Rossum6ac06b31998-09-21 14:44:34 +000060 \lineiii{P}{void *}{integer}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000061\end{tableiii}
62
Guido van Rossum6c4f0031995-03-07 10:14:09 +000063A format character may be preceded by an integral repeat count; e.g.\
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000064the format string \code{'4h'} means exactly the same as \code{'hhhh'}.
65
Guido van Rossume20aef51997-08-26 20:39:54 +000066Whitespace characters between formats are ignored; a count and its
67format must not contain whitespace though.
68
Fred Drakecf0fb8b1998-07-23 21:18:25 +000069For the \character{s} format character, the count is interpreted as the
Guido van Rossum12543461996-12-31 02:22:14 +000070size of the string, not a repeat count like for the other format
71characters; e.g. \code{'10s'} means a single 10-byte string, while
72\code{'10c'} means 10 characters. For packing, the string is
73truncated or padded with null bytes as appropriate to make it fit.
74For unpacking, the resulting string always has exactly the specified
75number of bytes. As a special case, \code{'0s'} means a single, empty
76string (while \code{'0c'} means 0 characters).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000077
Fred Drakecf0fb8b1998-07-23 21:18:25 +000078The \character{p} format character can be used to encode a Pascal
79string. The first byte is the length of the stored string, with the
80bytes of the string following. If count is given, it is used as the
81total number of bytes used, including the length byte. If the string
82passed in to \function{pack()} is too long, the stored representation
83is truncated. If the string is too short, padding is used to ensure
84that exactly enough bytes are used to satisfy the count.
85
86For the \character{I} and \character{L} format characters, the return
Guido van Rossum65307171997-01-03 19:21:53 +000087value is a Python long integer.
Guido van Rossum12543461996-12-31 02:22:14 +000088
Guido van Rossum6ac06b31998-09-21 14:44:34 +000089For the \character{P} format character, the return value is a Python
90integer or long integer, depending on the size needed to hold a
91pointer when it has been cast to an integer type. A NULL pointer will
92always be returned as the Python integer 0. When packing pointer-sized
93values, Python integer or long integer objects may be used. For
94example, the Alpha and Merced processors use 64-bit pointer values,
95meaning a Python long integer will be used to hold the pointer; other
96platforms use 32-bit pointers and will use a Python integer.
97
Guido van Rossum12543461996-12-31 02:22:14 +000098By default, C numbers are represented in the machine's native format
99and byte order, and properly aligned by skipping pad bytes if
100necessary (according to the rules used by the C compiler).
101
102Alternatively, the first character of the format string can be used to
103indicate the byte order, size and alignment of the packed data,
104according to the following table:
105
Fred Drakeee601911998-04-11 20:53:03 +0000106\begin{tableiii}{c|l|l}{samp}{Character}{Byte order}{Size and alignment}
Guido van Rossum12543461996-12-31 02:22:14 +0000107 \lineiii{@}{native}{native}
108 \lineiii{=}{native}{standard}
109 \lineiii{<}{little-endian}{standard}
110 \lineiii{>}{big-endian}{standard}
111 \lineiii{!}{network (= big-endian)}{standard}
112\end{tableiii}
113
Fred Drakecf0fb8b1998-07-23 21:18:25 +0000114If the first character is not one of these, \character{@} is assumed.
Guido van Rossum12543461996-12-31 02:22:14 +0000115
116Native byte order is big-endian or little-endian, depending on the
117host system (e.g. Motorola and Sun are big-endian; Intel and DEC are
118little-endian).
119
120Native size and alignment are determined using the C compiler's sizeof
121expression. This is always combined with native byte order.
122
123Standard size and alignment are as follows: no alignment is required
124for any type (so you have to use pad bytes); short is 2 bytes; int and
Guido van Rossumdbadd551997-01-03 04:20:09 +0000125long are 4 bytes. Float and double are 32-bit and 64-bit IEEE floating
126point numbers, respectively.
Guido van Rossum12543461996-12-31 02:22:14 +0000127
Fred Drakecf0fb8b1998-07-23 21:18:25 +0000128Note the difference between \character{@} and \character{=}: both use native
Guido van Rossum12543461996-12-31 02:22:14 +0000129byte order, but the size and alignment of the latter is standardized.
130
Fred Drakecf0fb8b1998-07-23 21:18:25 +0000131The form \character{!} is available for those poor souls who claim they
Guido van Rossum12543461996-12-31 02:22:14 +0000132can't remember whether network byte order is big-endian or
133little-endian.
134
135There is no way to indicate non-native byte order (i.e. force
Fred Drakecf0fb8b1998-07-23 21:18:25 +0000136byte-swapping); use the appropriate choice of \character{<} or
137\character{>}.
Guido van Rossum12543461996-12-31 02:22:14 +0000138
Guido van Rossum6ac06b31998-09-21 14:44:34 +0000139The \character{P} format character is only available for the native
140byte ordering (selected as the default or with the \character{@} byte
141order character). The byte order character \character{=} chooses to
142use little- or big-endian ordering based on the host system. The
143struct module does not interpret this as native ordering, so the
144\character{P} format is not available.
145
Guido van Rossum12543461996-12-31 02:22:14 +0000146Examples (all using native byte order, size and alignment, on a
147big-endian machine):
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000148
Fred Drake19479911998-02-13 06:58:54 +0000149\begin{verbatim}
Guido van Rossumdbadd551997-01-03 04:20:09 +0000150>>> from struct import *
151>>> pack('hhl', 1, 2, 3)
152'\000\001\000\002\000\000\000\003'
153>>> unpack('hhl', '\000\001\000\002\000\000\000\003')
154(1, 2, 3)
155>>> calcsize('hhl')
1568
157>>>
Fred Drake19479911998-02-13 06:58:54 +0000158\end{verbatim}
Guido van Rossume47da0a1997-07-17 16:34:52 +0000159%
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000160Hint: to align the end of a structure to the alignment requirement of
161a particular type, end the format with the code for that type with a
Guido van Rossum6c4f0031995-03-07 10:14:09 +0000162repeat count of zero, e.g.\ the format \code{'llh0l'} specifies two
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000163pad bytes at the end, assuming longs are aligned on 4-byte boundaries.
Fred Drake7ddd0431998-03-08 07:44:13 +0000164This only works when native size and alignment are in effect;
165standard size and alignment does not enforce any alignment.
166
167\begin{seealso}
168\seemodule{array}{packed binary storage of homogeneous data}
169\end{seealso}