mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-18 05:26:57 +08:00
349 lines
12 KiB
HTML
349 lines
12 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||
<html>
|
||
<head>
|
||
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
||
<title>将数据打包成二进制字符串</title>
|
||
</head>
|
||
<body class="docs"><div id="layout">
|
||
<div id="layout-content"><div id="function.pack" class="refentry">
|
||
<div class="refnamediv">
|
||
<h1 class="refname">pack</h1>
|
||
<p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">pack</span> — <span class="dc-title">将数据打包成二进制字符串</span></p>
|
||
|
||
</div>
|
||
|
||
<div class="refsect1 description" id="refsect1-function.pack-description">
|
||
<h3 class="title">说明</h3>
|
||
<div class="methodsynopsis dc-description">
|
||
<span class="methodname"><strong>pack</strong></span>
|
||
( <span class="methodparam"><span class="type">string</span> <code class="parameter">$format</code></span>
|
||
[, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$args</code></span>
|
||
[, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$...</code></span>
|
||
]] ) : <span class="type">string</span></div>
|
||
|
||
<p class="para rdfs-comment">
|
||
根据<code class="parameter">format</code>将给地的参数打包成二进制字符串。
|
||
</p>
|
||
<p class="para">
|
||
这个函数的思想来自Perl,所有格式化代码(<code class="parameter">format</code>)的工作原理都与Perl相同。 但是,缺少了部分格式代码,比如Perl的“u”。
|
||
</p>
|
||
<p class="para">
|
||
注意,有符号值和无符号值之间的区别只影响函数<span class="function"><a href="unpack.html" class="function">unpack()</a></span>, 在那些使用有符号和无符号格式代码的地方<span class="function"><strong>pack()</strong></span>函数产生相同的结果。
|
||
</p>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 parameters" id="refsect1-function.pack-parameters">
|
||
<h3 class="title">参数</h3>
|
||
<p class="para">
|
||
<dl>
|
||
|
||
|
||
<dt>
|
||
<code class="parameter">format</code></dt>
|
||
|
||
<dd>
|
||
|
||
<p class="para">
|
||
<code class="parameter">format</code>字符串由格式代码组成,后面跟着一个可选的重复参数。 重复参数可以是一个整数值或者<em>*</em>值来重复到输入数据的末尾。对于a, A, h, H格式化代码,其后的重复参数指定了给定数据将会被使用几个字符串,对于@,其后的数字表示放置剩余数据的绝对定位(之前的数据将会被空字符串填充),对于其他所有内容,重复数量指定消耗多少数据并将其打包到生成的二进制字符串中。
|
||
</p>
|
||
<p class="para">
|
||
目前已实现的格式如下:
|
||
<table class="doctable table">
|
||
<caption><strong><span class="function"><strong>pack()</strong></span> 格式字符</strong></caption>
|
||
|
||
<thead>
|
||
<tr>
|
||
<th>代码</th>
|
||
<th>描述</th>
|
||
</tr>
|
||
|
||
</thead>
|
||
|
||
<tbody class="tbody">
|
||
<tr>
|
||
<td>a</td>
|
||
<td>以NUL字节填充字符串</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td>A</td>
|
||
<td>以SPACE(空格)填充字符串</td></tr>
|
||
|
||
<tr>
|
||
<td>h</td>
|
||
<td>十六进制字符串,低位在前</td></tr>
|
||
|
||
<tr>
|
||
<td>H</td>
|
||
<td>十六进制字符串,高位在前</td></tr>
|
||
|
||
<tr><td>c</td><td>有符号字符</td></tr>
|
||
|
||
<tr>
|
||
<td>C</td>
|
||
<td>无符号字符</td></tr>
|
||
|
||
<tr>
|
||
<td>s</td>
|
||
<td>有符号短整型(16位,主机字节序)</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td>S</td>
|
||
<td>无符号短整型(16位,主机字节序)</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td>n</td>
|
||
<td>无符号短整型(16位,大端字节序)</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td>v</td>
|
||
<td>无符号短整型(16位,小端字节序)</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td>i</td>
|
||
<td>有符号整型(机器相关大小字节序)</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td>I</td>
|
||
<td>无符号整型(机器相关大小字节序)</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td>l</td>
|
||
<td>有符号长整型(32位,主机字节序)</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td>L</td>
|
||
<td>无符号长整型(32位,主机字节序)</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td>N</td>
|
||
<td>无符号长整型(32位,大端字节序)</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td>V</td>
|
||
<td>无符号长整型(32位,小端字节序)</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td>q</td>
|
||
<td>有符号长长整型(64位,主机字节序)</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td>Q</td>
|
||
<td>无符号长长整型(64位,主机字节序)</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td>J</td>
|
||
<td>无符号长长整型(64位,大端字节序)</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td>P</td>
|
||
<td>无符号长长整型(64位,小端字节序)</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td>f</td>
|
||
<td>单精度浮点型(机器相关大小)</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td>g</td>
|
||
<td>单精度浮点型(机器相关大小,小端字节序)</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td>G</td>
|
||
<td>单精度浮点型(机器相关大小,大端字节序)</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td>d</td>
|
||
<td>双精度浮点型(机器相关大小)</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td>e</td>
|
||
<td>双精度浮点型(机器相关大小,小端字节序)</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td>E</td>
|
||
<td>双精度浮点型(机器相关大小,大端字节序)</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td>x</td>
|
||
<td>NUL字节</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td>X</td>
|
||
<td>回退已字节</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td>Z</td>
|
||
<td>以NUL字节填充字符串空白(PHP 5.5中新加入的)</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td>@</td>
|
||
<td>NUL填充到绝对位置</td>
|
||
</tr>
|
||
|
||
</tbody>
|
||
|
||
</table>
|
||
|
||
</p>
|
||
</dd>
|
||
|
||
|
||
|
||
<dt>
|
||
<code class="parameter">args</code></dt>
|
||
|
||
<dd>
|
||
|
||
<p class="para">
|
||
</p>
|
||
</dd>
|
||
|
||
|
||
</dl>
|
||
|
||
</p>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 returnvalues" id="refsect1-function.pack-returnvalues">
|
||
<h3 class="title">返回值</h3>
|
||
<p class="para">
|
||
返回包含数据的二进制字符串。
|
||
</p>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 changelog" id="refsect1-function.pack-changelog">
|
||
<h3 class="title">更新日志</h3>
|
||
<p class="para">
|
||
<table class="doctable informaltable">
|
||
|
||
<thead>
|
||
<tr>
|
||
<th>版本</th>
|
||
<th>说明</th>
|
||
</tr>
|
||
|
||
</thead>
|
||
|
||
<tbody class="tbody">
|
||
<tr>
|
||
<td>7.2.0</td>
|
||
<td>
|
||
<span class="type"><a href="language.types.float.html" class="type float">float</a></span> 和 <span class="type"><a href="language.types.float.html" class="type double">double</a></span> 类型支持打断和小端。
|
||
</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td>7.0.15,7.1.1</td>
|
||
<td>
|
||
添加了"e","E","g"和"G"代码以启用float和double的字节顺序支持。
|
||
</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td>5.6.3</td>
|
||
<td>
|
||
添加了“q”、“q”、“J”和“P”代码以支持处理64位数字。
|
||
</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td>5.5.0</td>
|
||
<td>
|
||
“Z”代码添加了与“a”等效的功能,以实现Perl兼容性。
|
||
</td>
|
||
</tr>
|
||
|
||
</tbody>
|
||
|
||
</table>
|
||
|
||
</p>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 examples" id="refsect1-function.pack-examples">
|
||
<h3 class="title">范例</h3>
|
||
<p class="para">
|
||
<div class="example" id="example-4543">
|
||
<p><strong>Example #1 <span class="function"><strong>pack()</strong></span> 范例</strong></p>
|
||
<div class="example-contents">
|
||
<div class="phpcode"><pre><span style="color: #000000">
|
||
<span style="color: #0000BB"><?php<br />$binarydata </span><span style="color: #007700">= </span><span style="color: #0000BB">pack</span><span style="color: #007700">(</span><span style="color: #DD0000">"nvc*"</span><span style="color: #007700">, </span><span style="color: #0000BB">0x1234</span><span style="color: #007700">, </span><span style="color: #0000BB">0x5678</span><span style="color: #007700">, </span><span style="color: #0000BB">65</span><span style="color: #007700">, </span><span style="color: #0000BB">66</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
|
||
</span>
|
||
</pre></div>
|
||
</div>
|
||
|
||
<div class="example-contents"><p>
|
||
The resulting binary string will be 6 bytes long and contain
|
||
the byte sequence 0x12, 0x34, 0x78, 0x56, 0x41, 0x42.
|
||
</p></div>
|
||
</div>
|
||
</p>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 notes" id="refsect1-function.pack-notes">
|
||
<h3 class="title">注释</h3>
|
||
<div class="caution"><strong class="caution">Caution</strong>
|
||
<p class="para">Note that PHP internally stores <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> values as
|
||
signed values of a machine-dependent size (C type <em>long</em>).
|
||
Integer literals and operations that yield numbers outside the bounds of the
|
||
<span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> type will be stored as <span class="type"><a href="language.types.float.html" class="type float">float</a></span>.
|
||
When packing these floats as integers, they are first cast into the integer
|
||
type. This may or may not result in the desired byte pattern.
|
||
</p>
|
||
<p class="para">The most relevant case is when packing unsigned numbers that would
|
||
be representable with the <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> type if it were unsigned.
|
||
In systems where the <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> type has a 32-bit size, the cast
|
||
usually results in the same byte pattern as if the <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> were
|
||
unsigned (although this relies on implementation-defined unsigned to signed
|
||
conversions, as per the C standard). In systems where the
|
||
<span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> type has 64_bit size, the <span class="type"><a href="language.types.float.html" class="type float">float</a></span> most
|
||
likely does not have a mantissa large enough to hold the value without
|
||
loss of precision. If those systems also have a native 64-bit C
|
||
<em>int</em> type (most UNIX-like systems don't), the only way to
|
||
use the <em>I</em> pack format in the upper range is to create
|
||
<span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> negative values with the same byte representation
|
||
as the desired unsigned value.
|
||
</p>
|
||
</div>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 seealso" id="refsect1-function.pack-seealso">
|
||
<h3 class="title">参见</h3>
|
||
<p class="para">
|
||
<ul class="simplelist">
|
||
<li class="member"><span class="function"><a href="unpack.html" class="function" rel="rdfs-seeAlso">unpack()</a> - Unpack data from binary string</span></li>
|
||
</ul>
|
||
</p>
|
||
</div>
|
||
|
||
|
||
</div></div></div></body></html> |