⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 module-struct.html

📁 一本很好的python的说明书,适合对python感兴趣的人
💻 HTML
📖 第 1 页 / 共 2 页
字号:
e.g. the format string <code>'4h'</code> means exactly the same as
<code>'hhhh'</code>.

<P>
Whitespace characters between formats are ignored; a count and its
format must not contain whitespace though.

<P>
For the "<tt class="character">s</tt>" format character, the count is interpreted as the
size of the string, not a repeat count like for the other format
characters; e.g. <code>'10s'</code> means a single 10-byte string, while
<code>'10c'</code> means 10 characters.  For packing, the string is
truncated or padded with null bytes as appropriate to make it fit.
For unpacking, the resulting string always has exactly the specified
number of bytes.  As a special case, <code>'0s'</code> means a single, empty
string (while <code>'0c'</code> means 0 characters).

<P>
The "<tt class="character">p</tt>" format character can be used to encode a Pascal
string.  The first byte is the length of the stored string, with the
bytes of the string following.  If count is given, it is used as the
total number of bytes used, including the length byte.  If the string
passed in to <tt class="function">pack()</tt> is too long, the stored representation
is truncated.  If the string is too short, padding is used to ensure
that exactly enough bytes are used to satisfy the count.

<P>
For the "<tt class="character">I</tt>" and "<tt class="character">L</tt>" format characters, the return
value is a Python long integer.

<P>
For the "<tt class="character">P</tt>" format character, the return value is a Python
integer or long integer, depending on the size needed to hold a
pointer when it has been cast to an integer type.  A <tt class="constant">NULL</tt> pointer will
always be returned as the Python integer <code>0</code>. When packing pointer-sized
values, Python integer or long integer objects may be used.  For
example, the Alpha and Merced processors use 64-bit pointer values,
meaning a Python long integer will be used to hold the pointer; other
platforms use 32-bit pointers and will use a Python integer.

<P>
By default, 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>
Alternatively, the first character of the format string can be used to
indicate the byte order, size and alignment of the packed data,
according to the following table:

<P>
<table border align="center" style="border-collapse: collapse">
  <thead>
    <tr>
      <th align="center"><b>Character</b>&nbsp;</th>
      <th align="left"><b>Byte order</b>&nbsp;</th>
      <th align="left"><b>Size and alignment</b>&nbsp;</th>
    </thead>
  <tbody valign='baseline'>
    <tr><td align="center" valign="baseline"><samp>@</samp></td>
        <td align="left">native</td>
        <td align="left">native</td>
    <tr><td align="center" valign="baseline"><samp>=</samp></td>
        <td align="left">native</td>
        <td align="left">standard</td>
    <tr><td align="center" valign="baseline"><samp>&lt;</samp></td>
        <td align="left">little-endian</td>
        <td align="left">standard</td>
    <tr><td align="center" valign="baseline"><samp>&gt;</samp></td>
        <td align="left">big-endian</td>
        <td align="left">standard</td>
    <tr><td align="center" valign="baseline"><samp>!</samp></td>
        <td align="left">network (= big-endian)</td>
        <td align="left">standard</td></tbody>
</table>

<P>
If the first character is not one of these, "<tt class="character">@</tt>" is assumed.

<P>
Native byte order is big-endian or little-endian, depending on the
host system (e.g. Motorola and Sun are big-endian; Intel and DEC are
little-endian).

<P>
Native size and alignment are determined using the C compiler's
<tt class="keyword">sizeof</tt> expression.  This is always combined with native byte
order.

<P>
Standard size and alignment are as follows: no alignment is required
for any type (so you have to use pad bytes); <tt class="ctype">short</tt> is 2 bytes;
<tt class="ctype">int</tt> and <tt class="ctype">long</tt> are 4 bytes.  <tt class="ctype">float</tt> and
<tt class="ctype">double</tt> are 32-bit and 64-bit IEEE floating point numbers,
respectively.

<P>
Note the difference between "<tt class="character">@</tt>" and "<tt class="character">=</tt>": both use
native byte order, but the size and alignment of the latter is
standardized.

<P>
The form "<tt class="character">!</tt>" is available for those poor souls who claim they
can't remember whether network byte order is big-endian or
little-endian.

<P>
There is no way to indicate non-native byte order (i.e. force
byte-swapping); use the appropriate choice of "<tt class="character">&lt;</tt>" or
"<tt class="character">&gt;</tt>".

<P>
The "<tt class="character">P</tt>" format character is only available for the native
byte ordering (selected as the default or with the "<tt class="character">@</tt>" byte
order character). The byte order character "<tt class="character">=</tt>" chooses to
use little- or big-endian ordering based on the host system. The
struct module does not interpret this as native ordering, so the
"<tt class="character">P</tt>" format is not available.

<P>
Examples (all using native byte order, size and alignment, on a
big-endian machine):

<P>
<dl><dd><pre class="verbatim">
&gt;&gt;&gt; from struct import *
&gt;&gt;&gt; pack('hhl', 1, 2, 3)
'\000\001\000\002\000\000\000\003'
&gt;&gt;&gt; unpack('hhl', '\000\001\000\002\000\000\000\003')
(1, 2, 3)
&gt;&gt;&gt; calcsize('hhl')
8
</pre></dl>

<P>
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.
This only works when native size and alignment are in effect;
standard size and alignment does not enforce any alignment.

<P>
<div class='seealso'>
  <p class='heading'><b>See Also:</b></p>

  <dl compact class="seemodule">
    <dt>Module <b><tt class='module'><a href="module-array.html" tppabs="http://www.python.org/doc/current/lib/module-array.html">array</a></tt>:</b>
    <dd>Packed binary storage of homogeneous data.
  </dl>
  <dl compact class="seemodule">
    <dt>Module <b><tt class='module'><a href="module-xdrlib.html" tppabs="http://www.python.org/doc/current/lib/module-xdrlib.html">xdrlib</a></tt>:</b>
    <dd>Packing and unpacking of XDR data.
  </dl>
</div>

<DIV CLASS="navigation"><p><hr><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="match-objects.html" tppabs="http://www.python.org/doc/current/lib/match-objects.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
  alt="Previous Page" width="32"></A></td>
<td><A href="strings.html" tppabs="http://www.python.org/doc/current/lib/strings.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
  alt="Up One Level" width="32"></A></td>
<td><A href="module-fpformat.html" tppabs="http://www.python.org/doc/current/lib/module-fpformat.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
  alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
  alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
  alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
  alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="match-objects.html" tppabs="http://www.python.org/doc/current/lib/match-objects.html">4.2.5 Match Objects</A>
<b class="navlabel">Up:</b> <a class="sectref" href="strings.html" tppabs="http://www.python.org/doc/current/lib/strings.html">4. String Services</A>
<b class="navlabel">Next:</b> <a class="sectref" href="module-fpformat.html" tppabs="http://www.python.org/doc/current/lib/module-fpformat.html">4.4 fpformat  </A>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>See <i><a href="about.html" tppabs="http://www.python.org/doc/current/lib/about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -