blob: be7d80bd0d881b98586cc2e279d2059c23f60761 [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 Rossum5fdeeea1994-01-02 01:22:07 +000060\end{tableiii}
61
Guido van Rossum6c4f0031995-03-07 10:14:09 +000062A format character may be preceded by an integral repeat count; e.g.\
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000063the format string \code{'4h'} means exactly the same as \code{'hhhh'}.
64
Guido van Rossume20aef51997-08-26 20:39:54 +000065Whitespace characters between formats are ignored; a count and its
66format must not contain whitespace though.
67
Fred Drakecf0fb8b1998-07-23 21:18:25 +000068For the \character{s} format character, the count is interpreted as the
Guido van Rossum12543461996-12-31 02:22:14 +000069size of the string, not a repeat count like for the other format
70characters; e.g. \code{'10s'} means a single 10-byte string, while
71\code{'10c'} means 10 characters. For packing, the string is
72truncated or padded with null bytes as appropriate to make it fit.
73For unpacking, the resulting string always has exactly the specified
74number of bytes. As a special case, \code{'0s'} means a single, empty
75string (while \code{'0c'} means 0 characters).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000076
Fred Drakecf0fb8b1998-07-23 21:18:25 +000077The \character{p} format character can be used to encode a Pascal
78string. The first byte is the length of the stored string, with the
79bytes of the string following. If count is given, it is used as the
80total number of bytes used, including the length byte. If the string
81passed in to \function{pack()} is too long, the stored representation
82is truncated. If the string is too short, padding is used to ensure
83that exactly enough bytes are used to satisfy the count.
84
85For the \character{I} and \character{L} format characters, the return
Guido van Rossum65307171997-01-03 19:21:53 +000086value is a Python long integer.
Guido van Rossum12543461996-12-31 02:22:14 +000087
88By default, C numbers are represented in the machine's native format
89and byte order, and properly aligned by skipping pad bytes if
90necessary (according to the rules used by the C compiler).
91
92Alternatively, the first character of the format string can be used to
93indicate the byte order, size and alignment of the packed data,
94according to the following table:
95
Fred Drakeee601911998-04-11 20:53:03 +000096\begin{tableiii}{c|l|l}{samp}{Character}{Byte order}{Size and alignment}
Guido van Rossum12543461996-12-31 02:22:14 +000097 \lineiii{@}{native}{native}
98 \lineiii{=}{native}{standard}
99 \lineiii{<}{little-endian}{standard}
100 \lineiii{>}{big-endian}{standard}
101 \lineiii{!}{network (= big-endian)}{standard}
102\end{tableiii}
103
Fred Drakecf0fb8b1998-07-23 21:18:25 +0000104If the first character is not one of these, \character{@} is assumed.
Guido van Rossum12543461996-12-31 02:22:14 +0000105
106Native byte order is big-endian or little-endian, depending on the
107host system (e.g. Motorola and Sun are big-endian; Intel and DEC are
108little-endian).
109
110Native size and alignment are determined using the C compiler's sizeof
111expression. This is always combined with native byte order.
112
113Standard size and alignment are as follows: no alignment is required
114for any type (so you have to use pad bytes); short is 2 bytes; int and
Guido van Rossumdbadd551997-01-03 04:20:09 +0000115long are 4 bytes. Float and double are 32-bit and 64-bit IEEE floating
116point numbers, respectively.
Guido van Rossum12543461996-12-31 02:22:14 +0000117
Fred Drakecf0fb8b1998-07-23 21:18:25 +0000118Note the difference between \character{@} and \character{=}: both use native
Guido van Rossum12543461996-12-31 02:22:14 +0000119byte order, but the size and alignment of the latter is standardized.
120
Fred Drakecf0fb8b1998-07-23 21:18:25 +0000121The form \character{!} is available for those poor souls who claim they
Guido van Rossum12543461996-12-31 02:22:14 +0000122can't remember whether network byte order is big-endian or
123little-endian.
124
125There is no way to indicate non-native byte order (i.e. force
Fred Drakecf0fb8b1998-07-23 21:18:25 +0000126byte-swapping); use the appropriate choice of \character{<} or
127\character{>}.
Guido van Rossum12543461996-12-31 02:22:14 +0000128
129Examples (all using native byte order, size and alignment, on a
130big-endian machine):
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000131
Fred Drake19479911998-02-13 06:58:54 +0000132\begin{verbatim}
Guido van Rossumdbadd551997-01-03 04:20:09 +0000133>>> from struct import *
134>>> pack('hhl', 1, 2, 3)
135'\000\001\000\002\000\000\000\003'
136>>> unpack('hhl', '\000\001\000\002\000\000\000\003')
137(1, 2, 3)
138>>> calcsize('hhl')
1398
140>>>
Fred Drake19479911998-02-13 06:58:54 +0000141\end{verbatim}
Guido van Rossume47da0a1997-07-17 16:34:52 +0000142%
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000143Hint: to align the end of a structure to the alignment requirement of
144a particular type, end the format with the code for that type with a
Guido van Rossum6c4f0031995-03-07 10:14:09 +0000145repeat count of zero, e.g.\ the format \code{'llh0l'} specifies two
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000146pad bytes at the end, assuming longs are aligned on 4-byte boundaries.
Fred Drake7ddd0431998-03-08 07:44:13 +0000147This only works when native size and alignment are in effect;
148standard size and alignment does not enforce any alignment.
149
150\begin{seealso}
151\seemodule{array}{packed binary storage of homogeneous data}
152\end{seealso}