1 \section{\module{struct
} ---
2 Interpret strings as packed binary data
}
3 \declaremodule{builtin
}{struct
}
5 \modulesynopsis{Interpret strings as packed binary data.
}
7 \indexii{C
}{structures
}
8 \indexiii{packing
}{binary
}{data
}
10 This module performs conversions between Python values and C
11 structs represented as Python strings. It uses
\dfn{format strings
}
12 (explained below) as compact descriptions of the lay-out of the C
13 structs and the intended conversion to/from Python values. This can
14 be used in handling binary data stored in files or from network
15 connections, among other sources.
17 The module defines the following exception and functions:
20 \begin{excdesc
}{error
}
21 Exception raised on various occasions; argument is a string
22 describing what is wrong.
25 \begin{funcdesc
}{pack
}{fmt, v1, v2,
\textrm{\ldots}}
26 Return a string containing the values
27 \code{\var{v1
},
\var{v2
},
\textrm{\ldots}} packed according to the given
28 format. The arguments must match the values required by the format
32 \begin{funcdesc
}{unpack
}{fmt, string
}
33 Unpack the string (presumably packed by
\code{pack(
\var{fmt
},
34 \textrm{\ldots})
}) according to the given format. The result is a
35 tuple even if it contains exactly one item. The string must contain
36 exactly the amount of data required by the format (i.e.
37 \code{len(
\var{string
})
} must equal
\code{calcsize(
\var{fmt
})
}).
40 \begin{funcdesc
}{calcsize
}{fmt
}
41 Return the size of the struct (and hence of the string)
42 corresponding to the given format.
45 Format characters have the following meaning; the conversion between
46 C and Python values should be obvious given their types:
48 \begin{tableiii
}{c|l|l
}{samp
}{Format
}{C Type
}{Python
}
49 \lineiii{x
}{pad byte
}{no value
}
50 \lineiii{c
}{\ctype{char
}}{string of length
1}
51 \lineiii{b
}{\ctype{signed char
}}{integer
}
52 \lineiii{B
}{\ctype{unsigned char
}}{integer
}
53 \lineiii{h
}{\ctype{short
}}{integer
}
54 \lineiii{H
}{\ctype{unsigned short
}}{integer
}
55 \lineiii{i
}{\ctype{int
}}{integer
}
56 \lineiii{I
}{\ctype{unsigned int
}}{integer
}
57 \lineiii{l
}{\ctype{long
}}{integer
}
58 \lineiii{L
}{\ctype{unsigned long
}}{integer
}
59 \lineiii{f
}{\ctype{float
}}{float
}
60 \lineiii{d
}{\ctype{double
}}{float
}
61 \lineiii{s
}{\ctype{char
[]}}{string
}
62 \lineiii{p
}{\ctype{char
[]}}{string
}
63 \lineiii{P
}{\ctype{void *
}}{integer
}
66 A format character may be preceded by an integral repeat count;
67 e.g.\ the format string
\code{'
4h'
} means exactly the same as
70 Whitespace characters between formats are ignored; a count and its
71 format must not contain whitespace though.
73 For the
\character{s
} format character, the count is interpreted as the
74 size of the string, not a repeat count like for the other format
75 characters; e.g.
\code{'
10s'
} means a single
10-byte string, while
76 \code{'
10c'
} means
10 characters. For packing, the string is
77 truncated or padded with null bytes as appropriate to make it fit.
78 For unpacking, the resulting string always has exactly the specified
79 number of bytes. As a special case,
\code{'
0s'
} means a single, empty
80 string (while
\code{'
0c'
} means
0 characters).
82 The
\character{p
} format character can be used to encode a Pascal
83 string. The first byte is the length of the stored string, with the
84 bytes of the string following. If count is given, it is used as the
85 total number of bytes used, including the length byte. If the string
86 passed in to
\function{pack()
} is too long, the stored representation
87 is truncated. If the string is too short, padding is used to ensure
88 that exactly enough bytes are used to satisfy the count.
90 For the
\character{I
} and
\character{L
} format characters, the return
91 value is a Python long integer.
93 For the
\character{P
} format character, the return value is a Python
94 integer or long integer, depending on the size needed to hold a
95 pointer when it has been cast to an integer type. A
\NULL{} pointer will
96 always be returned as the Python integer
\code{0}. When packing pointer-sized
97 values, Python integer or long integer objects may be used. For
98 example, the Alpha and Merced processors use
64-bit pointer values,
99 meaning a Python long integer will be used to hold the pointer; other
100 platforms use
32-bit pointers and will use a Python integer.
102 By default, C numbers are represented in the machine's native format
103 and byte order, and properly aligned by skipping pad bytes if
104 necessary (according to the rules used by the C compiler).
106 Alternatively, the first character of the format string can be used to
107 indicate the byte order, size and alignment of the packed data,
108 according to the following table:
110 \begin{tableiii
}{c|l|l
}{samp
}{Character
}{Byte order
}{Size and alignment
}
111 \lineiii{@
}{native
}{native
}
112 \lineiii{=
}{native
}{standard
}
113 \lineiii{<
}{little-endian
}{standard
}
114 \lineiii{>
}{big-endian
}{standard
}
115 \lineiii{!
}{network (= big-endian)
}{standard
}
118 If the first character is not one of these,
\character{@
} is assumed.
120 Native byte order is big-endian or little-endian, depending on the
121 host system (e.g. Motorola and Sun are big-endian; Intel and DEC are
124 Native size and alignment are determined using the C compiler's
125 \keyword{sizeof
} expression. This is always combined with native byte
128 Standard size and alignment are as follows: no alignment is required
129 for any type (so you have to use pad bytes);
\ctype{short
} is
2 bytes;
130 \ctype{int
} and
\ctype{long
} are
4 bytes.
\ctype{float
} and
131 \ctype{double
} are
32-bit and
64-bit IEEE floating point numbers,
134 Note the difference between
\character{@
} and
\character{=
}: both use
135 native byte order, but the size and alignment of the latter is
138 The form
\character{!
} is available for those poor souls who claim they
139 can't remember whether network byte order is big-endian or
142 There is no way to indicate non-native byte order (i.e. force
143 byte-swapping); use the appropriate choice of
\character{<
} or
146 The
\character{P
} format character is only available for the native
147 byte ordering (selected as the default or with the
\character{@
} byte
148 order character). The byte order character
\character{=
} chooses to
149 use little- or big-endian ordering based on the host system. The
150 struct module does not interpret this as native ordering, so the
151 \character{P
} format is not available.
153 Examples (all using native byte order, size and alignment, on a
157 >>> from struct import *
158 >>> pack('hhl',
1,
2,
3)
159 '
\000\001\000\002\000\000\000\003'
160 >>> unpack('hhl', '
\000\001\000\002\000\000\000\003')
166 Hint: to align the end of a structure to the alignment requirement of
167 a particular type, end the format with the code for that type with a
168 repeat count of zero, e.g.\ the format
\code{'llh0l'
} specifies two
169 pad bytes at the end, assuming longs are aligned on
4-byte boundaries.
170 This only works when native size and alignment are in effect;
171 standard size and alignment does not enforce any alignment.
174 \seemodule{array
}{Packed binary storage of homogeneous data.
}
175 \seemodule{xdrlib
}{Packing and unpacking of XDR data.
}