<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >pack</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="PHP 手册" HREF="index.html"><LINK REL="UP" TITLE="Miscellaneous Functions" HREF="ref.misc.html"><LINK REL="PREVIOUS" TITLE="ignore_user_abort" HREF="function.ignore-user-abort.html"><LINK REL="NEXT" TITLE="php_check_syntax" HREF="function.php-check-syntax.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP 手册</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.ignore-user-abort.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.php-check-syntax.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.pack" ></A >pack</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN125771" ></A ><P > (PHP 3, PHP 4, PHP 5)</P >pack -- Pack data into binary string</DIV ><DIV CLASS="refsect1" ><A NAME="AEN125774" ></A ><H2 >说明</H2 >string <B CLASS="methodname" >pack</B > ( string format [, mixed args [, mixed ...]] )<BR ></BR ><P > Pack given arguments into binary string according to <CODE CLASS="parameter" >format</CODE >. </P ><P > The idea to this function was taken from Perl and all formatting codes work the same as there, however, there are some formatting codes that are missing such as Perl's "u" format code. </P ><P > Note that the distinction between signed and unsigned values only affects the function <A HREF="function.unpack.html" ><B CLASS="function" >unpack()</B ></A >, where as function <B CLASS="function" >pack()</B > gives the same result for signed and unsigned format codes. </P ><P > Also note that PHP internally stores <A HREF="language.types.integer.html" ><B CLASS="type" >integer</B ></A > values as signed values of a machine dependent size. If you give it an unsigned integer value too large to be stored that way it is converted to a <A HREF="language.types.float.html" ><B CLASS="type" >float</B ></A > which often yields an undesired result. </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN125797" ></A ><H2 >参数</H2 ><P > <P ></P ><DIV CLASS="variablelist" ><DL ><DT ><CODE CLASS="parameter" >format</CODE ></DT ><DD ><P > The <CODE CLASS="parameter" >format</CODE > string consists of format codes followed by an optional repeater argument. The repeater argument can be either an integer value or <TT CLASS="literal" >*</TT > for repeating to the end of the input data. For a, A, h, H the repeat count specifies how many characters of one data argument are taken, for @ it is the absolute position where to put the next data, for everything else the repeat count specifies how many data arguments are consumed and packed into the resulting binary string. </P ><P > Currently implemented formats are: <DIV CLASS="table" ><A NAME="AEN125809" ></A ><P ><B >表 1. <B CLASS="function" >pack()</B > format characters</B ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><COL><COL><THEAD ><TR ><TH >Code</TH ><TH >Description</TH ></TR ></THEAD ><TBODY ><TR ><TD >a</TD ><TD >NUL-padded string</TD ></TR ><TR ><TD >A</TD ><TD >SPACE-padded string</TD ></TR ><TR ><TD >h</TD ><TD >Hex string, low nibble first</TD ></TR ><TR ><TD >H</TD ><TD >Hex string, high nibble first</TD ></TR ><TR ><TD >c</TD ><TD >signed char</TD ></TR ><TR ><TD >C</TD ><TD >unsigned char</TD ></TR ><TR ><TD >s</TD ><TD >signed short (always 16 bit, machine byte order)</TD ></TR ><TR ><TD >S</TD ><TD >unsigned short (always 16 bit, machine byte order)</TD ></TR ><TR ><TD >n</TD ><TD >unsigned short (always 16 bit, big endian byte order)</TD ></TR ><TR ><TD >v</TD ><TD >unsigned short (always 16 bit, little endian byte order)</TD ></TR ><TR ><TD >i</TD ><TD >signed integer (machine dependent size and byte order)</TD ></TR ><TR ><TD >I</TD ><TD >unsigned integer (machine dependent size and byte order)</TD ></TR ><TR ><TD >l</TD ><TD >signed long (always 32 bit, machine byte order)</TD ></TR ><TR ><TD >L</TD ><TD >unsigned long (always 32 bit, machine byte order)</TD ></TR ><TR ><TD >N</TD ><TD >unsigned long (always 32 bit, big endian byte order)</TD ></TR ><TR ><TD >V</TD ><TD >unsigned long (always 32 bit, little endian byte order)</TD ></TR ><TR ><TD >f</TD ><TD >float (machine dependent size and representation)</TD ></TR ><TR ><TD >d</TD ><TD >double (machine dependent size and representation)</TD ></TR ><TR ><TD >x</TD ><TD >NUL byte</TD ></TR ><TR ><TD >X</TD ><TD >Back up one byte</TD ></TR ><TR ><TD >@</TD ><TD >NUL-fill to absolute position</TD ></TR ></TBODY ></TABLE ></DIV > </P ></DD ><DT ><CODE CLASS="parameter" >args</CODE ></DT ><DD ><P > </P ></DD ></DL ></DIV > </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN125886" ></A ><H2 >返回值</H2 ><P > Returns a binary string containing data. </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN125889" ></A ><H2 >范例</H2 ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN125892" ></A ><P ><B >例 1. <B CLASS="function" >pack()</B > example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$binarydata </font><font color="#007700">= </font><font color="#0000BB">pack</font><font color="#007700">(</font><font color="#DD0000">"nvc*"</font><font color="#007700">, </font><font color="#0000BB">0x1234</font><font color="#007700">, </font><font color="#0000BB">0x5678</font><font color="#007700">, </font><font color="#0000BB">65</font><font color="#007700">, </font><font color="#0000BB">66</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P > The resulting binary string will be 6 bytes long and contain the byte sequence 0x12, 0x34, 0x78, 0x56, 0x41, 0x42. </P ></DIV ></TD ></TR ></TABLE > </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN125897" ></A ><H2 >参见</H2 ><P > <P ></P ><TABLE BORDER="0" ><TBODY ><TR ><TD ><A HREF="function.unpack.html" ><B CLASS="function" >unpack()</B ></A ></TD ></TR ></TBODY ></TABLE ><P ></P > </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.ignore-user-abort.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >起始页</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.php-check-syntax.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >ignore_user_abort</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.misc.html" ACCESSKEY="U" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >php_check_syntax</TD ></TR ></TABLE ></DIV ></BODY ></HTML >