home *** CD-ROM | disk | FTP | other *** search
- <TITLE>struct -- Python library reference</TITLE>
- Prev: <A HREF="../r/regsub" TYPE="Prev">regsub</A>
- Up: <A HREF="../s/string_services" TYPE="Up">String Services</A>
- Top: <A HREF="../t/top" TYPE="Top">Top</A>
- <H1>4.4. Built-in Module <CODE>struct</CODE></H1>
- This module performs conversions between Python values and C
- structs represented as Python strings. It uses <DFN>format strings</DFN>
- (explained below) as compact descriptions of the lay-out of the C
- structs and the intended conversion to/from Python values.
- <P>
- See also built-in module <CODE>array</CODE>.
- The module defines the following exception and functions:
- <P>
- <DL><DT><B>error</B> -- exception of module struct<DD>
- Exception raised on various occasions; argument is a string
- describing what is wrong.
- </DL>
- <DL><DT><B>pack</B> (<VAR>fmt</VAR>, <VAR>v1</VAR>, <VAR>v2</VAR>, @varr@vardots) -- function of module struct<DD>
- Return a string containing the values
- <CODE><VAR>v1</VAR>, <VAR>v2</VAR>, <R>...</R></CODE> packed according to the given
- format. The arguments must match the values required by the format
- exactly.
- </DL>
- <DL><DT><B>unpack</B> (<VAR>fmt</VAR>, <VAR>string</VAR>) -- function of module struct<DD>
- Unpack the string (presumably packed by <CODE>pack(<VAR>fmt</VAR>, <R>...</R>)</CODE>)
- 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. <CODE>len(<VAR>string</VAR>)</CODE> must
- equal <CODE>calcsize(<VAR>fmt</VAR>)</CODE>).
- </DL>
- <DL><DT><B>calcsize</B> (<VAR>fmt</VAR>) -- function of module struct<DD>
- Return the size of the struct (and hence of the string)
- corresponding to the given format.
- </DL>
- Format characters have the following meaning; the conversion between C
- and Python values should be obvious given their types:
- <P>
- <DL>
- <DT><I>Format</I><DD><I>C</I> --- <I>Python</I>
- <P>
- <DT>`<SAMP>x</SAMP>'<DD>pad byte --- no value
- <DT>`<SAMP>c</SAMP>'<DD>char --- string of length 1
- <DT>`<SAMP>b</SAMP>'<DD>signed char --- integer
- <DT>`<SAMP>h</SAMP>'<DD>short --- integer
- <DT>`<SAMP>i</SAMP>'<DD>int --- integer
- <DT>`<SAMP>l</SAMP>'<DD>long --- integer
- <DT>`<SAMP>f</SAMP>'<DD>float --- float
- <DT>`<SAMP>d</SAMP>'<DD>double --- float
- </DL>
- A format character may be preceded by an integral repeat count; e.g.
- the format string <CODE>'4h'</CODE> means exactly the same as <CODE>'hhhh'</CODE>.
- <P>
- 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).
- <P>
- Examples (all on a big-endian machine):
- <P>
- <UL COMPACT><CODE>pack('hhl', 1, 2, 3) == '\000\001\000\002\000\000\000\003'<P>
- unpack('hhl', '\000\001\000\002\000\000\000\003') == (1, 2, 3)<P>
- calcsize('hhl') == 8<P>
- </CODE></UL>
- Hint: to align the end of a structure to the alignment requirement of
- a particular type, end the format with the code for that type with a
- repeat count of zero, e.g. the format <CODE>'llh0l'</CODE> specifies two
- pad bytes at the end, assuming longs are aligned on 4-byte boundaries.
- <P>
- (More format characters are planned, e.g. <CODE>'s'</CODE> 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.)
-