blob: 22595cb41b80f57b66d72e8b62b62fedaf105212 [file] [log] [blame]
Fred Drake3a0351c1998-04-04 07:23:21 +00001\section{Built-in Module \module{struct}}
Fred Drakeb91e9341998-07-23 17:59:49 +00002\declaremodule{builtin}{struct}
3
4\modulesynopsis{Interpret strings as packed binary data.}
5
Fred Drakeabdea221998-03-16 05:22:08 +00006\indexii{C@\C{}}{structures}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00007
8This module performs conversions between Python values and C
9structs represented as Python strings. It uses \dfn{format strings}
10(explained below) as compact descriptions of the lay-out of the C
11structs and the intended conversion to/from Python values.
12
13The module defines the following exception and functions:
14
Fred Drake7ddd0431998-03-08 07:44:13 +000015
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000016\begin{excdesc}{error}
17 Exception raised on various occasions; argument is a string
18 describing what is wrong.
19\end{excdesc}
20
Fred Drakecce10901998-03-17 06:33:25 +000021\begin{funcdesc}{pack}{fmt, v1, v2, {\rm \ldots}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000022 Return a string containing the values
23 \code{\var{v1}, \var{v2}, {\rm \ldots}} packed according to the given
24 format. The arguments must match the values required by the format
25 exactly.
26\end{funcdesc}
27
Fred Drakecce10901998-03-17 06:33:25 +000028\begin{funcdesc}{unpack}{fmt, string}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000029 Unpack the string (presumably packed by \code{pack(\var{fmt}, {\rm \ldots})})
30 according to the given format. The result is a tuple even if it
31 contains exactly one item. The string must contain exactly the
32 amount of data required by the format (i.e. \code{len(\var{string})} must
33 equal \code{calcsize(\var{fmt})}).
34\end{funcdesc}
35
36\begin{funcdesc}{calcsize}{fmt}
37 Return the size of the struct (and hence of the string)
38 corresponding to the given format.
39\end{funcdesc}
40
41Format characters have the following meaning; the conversion between C
42and Python values should be obvious given their types:
43
Fred Drakeee601911998-04-11 20:53:03 +000044\begin{tableiii}{c|l|l}{samp}{Format}{C Type}{Python}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000045 \lineiii{x}{pad byte}{no value}
46 \lineiii{c}{char}{string of length 1}
47 \lineiii{b}{signed char}{integer}
Guido van Rossum12543461996-12-31 02:22:14 +000048 \lineiii{B}{unsigned char}{integer}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000049 \lineiii{h}{short}{integer}
Guido van Rossum12543461996-12-31 02:22:14 +000050 \lineiii{H}{unsigned short}{integer}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000051 \lineiii{i}{int}{integer}
Guido van Rossum12543461996-12-31 02:22:14 +000052 \lineiii{I}{unsigned int}{integer}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000053 \lineiii{l}{long}{integer}
Guido van Rossum12543461996-12-31 02:22:14 +000054 \lineiii{L}{unsigned long}{integer}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000055 \lineiii{f}{float}{float}
56 \lineiii{d}{double}{float}
Guido van Rossum12543461996-12-31 02:22:14 +000057 \lineiii{s}{char[]}{string}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000058\end{tableiii}
59
Guido van Rossum6c4f0031995-03-07 10:14:09 +000060A format character may be preceded by an integral repeat count; e.g.\
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000061the format string \code{'4h'} means exactly the same as \code{'hhhh'}.
62
Guido van Rossume20aef51997-08-26 20:39:54 +000063Whitespace characters between formats are ignored; a count and its
64format must not contain whitespace though.
65
Guido van Rossum12543461996-12-31 02:22:14 +000066For the \code{'s'} format character, the count is interpreted as the
67size of the string, not a repeat count like for the other format
68characters; e.g. \code{'10s'} means a single 10-byte string, while
69\code{'10c'} means 10 characters. For packing, the string is
70truncated or padded with null bytes as appropriate to make it fit.
71For unpacking, the resulting string always has exactly the specified
72number of bytes. As a special case, \code{'0s'} means a single, empty
73string (while \code{'0c'} means 0 characters).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000074
Guido van Rossum12543461996-12-31 02:22:14 +000075For the \code{'I'} and \code{'L'} format characters, the return
Guido van Rossum65307171997-01-03 19:21:53 +000076value is a Python long integer.
Guido van Rossum12543461996-12-31 02:22:14 +000077
78By default, C numbers are represented in the machine's native format
79and byte order, and properly aligned by skipping pad bytes if
80necessary (according to the rules used by the C compiler).
81
82Alternatively, the first character of the format string can be used to
83indicate the byte order, size and alignment of the packed data,
84according to the following table:
85
Fred Drakeee601911998-04-11 20:53:03 +000086\begin{tableiii}{c|l|l}{samp}{Character}{Byte order}{Size and alignment}
Guido van Rossum12543461996-12-31 02:22:14 +000087 \lineiii{@}{native}{native}
88 \lineiii{=}{native}{standard}
89 \lineiii{<}{little-endian}{standard}
90 \lineiii{>}{big-endian}{standard}
91 \lineiii{!}{network (= big-endian)}{standard}
92\end{tableiii}
93
94If the first character is not one of these, \code{'@'} is assumed.
95
96Native byte order is big-endian or little-endian, depending on the
97host system (e.g. Motorola and Sun are big-endian; Intel and DEC are
98little-endian).
99
100Native size and alignment are determined using the C compiler's sizeof
101expression. This is always combined with native byte order.
102
103Standard size and alignment are as follows: no alignment is required
104for any type (so you have to use pad bytes); short is 2 bytes; int and
Guido van Rossumdbadd551997-01-03 04:20:09 +0000105long are 4 bytes. Float and double are 32-bit and 64-bit IEEE floating
106point numbers, respectively.
Guido van Rossum12543461996-12-31 02:22:14 +0000107
108Note the difference between \code{'@'} and \code{'='}: both use native
109byte order, but the size and alignment of the latter is standardized.
110
111The form \code{'!'} is available for those poor souls who claim they
112can't remember whether network byte order is big-endian or
113little-endian.
114
115There is no way to indicate non-native byte order (i.e. force
116byte-swapping); use the appropriate choice of \code{'<'} or
117\code{'>'}.
118
119Examples (all using native byte order, size and alignment, on a
120big-endian machine):
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000121
Fred Drake19479911998-02-13 06:58:54 +0000122\begin{verbatim}
Guido van Rossumdbadd551997-01-03 04:20:09 +0000123>>> from struct import *
124>>> pack('hhl', 1, 2, 3)
125'\000\001\000\002\000\000\000\003'
126>>> unpack('hhl', '\000\001\000\002\000\000\000\003')
127(1, 2, 3)
128>>> calcsize('hhl')
1298
130>>>
Fred Drake19479911998-02-13 06:58:54 +0000131\end{verbatim}
Guido van Rossume47da0a1997-07-17 16:34:52 +0000132%
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000133Hint: to align the end of a structure to the alignment requirement of
134a particular type, end the format with the code for that type with a
Guido van Rossum6c4f0031995-03-07 10:14:09 +0000135repeat count of zero, e.g.\ the format \code{'llh0l'} specifies two
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000136pad bytes at the end, assuming longs are aligned on 4-byte boundaries.
Fred Drake7ddd0431998-03-08 07:44:13 +0000137This only works when native size and alignment are in effect;
138standard size and alignment does not enforce any alignment.
139
140\begin{seealso}
141\seemodule{array}{packed binary storage of homogeneous data}
142\end{seealso}