(PHP 4, PHP 5, PHP 7)
pack — 锟斤拷锟斤拷锟捷达拷锟斤拷啥锟斤拷锟斤拷锟斤拷址锟斤拷锟�
锟斤拷锟斤拷format
锟斤拷锟斤拷锟截的诧拷锟斤拷锟斤拷锟斤拷啥锟斤拷锟斤拷锟斤拷址锟斤拷锟斤拷锟�
锟斤拷锟斤拷锟斤拷锟斤拷锟剿硷拷锟斤拷锟斤拷锟絇erl锟斤拷锟斤拷锟叫革拷式锟斤拷锟斤拷锟诫(format
锟斤拷锟侥癸拷锟斤拷原锟斤拷锟斤拷Perl锟斤拷同锟斤拷 锟斤拷锟角o拷缺锟斤拷锟剿诧拷锟街革拷式锟斤拷锟诫,锟斤拷锟斤拷Perl锟斤拷"u"锟斤拷
注锟解,锟叫凤拷锟斤拷值锟斤拷锟睫凤拷锟斤拷值之锟斤拷锟斤拷锟斤拷锟街挥帮拷旌拷锟�unpack(), 锟斤拷锟斤拷些使锟斤拷锟叫凤拷锟脚猴拷锟睫凤拷锟脚革拷式锟斤拷锟斤拷牡胤锟�pack()锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷同锟侥斤拷锟斤拷锟�
format
format
锟街凤拷锟斤拷锟缴革拷式锟斤拷锟斤拷锟斤拷桑锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷选锟斤拷锟截革拷锟斤拷锟斤拷锟斤拷 锟截革拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷值锟斤拷锟斤拷*值锟斤拷锟截革拷锟斤拷锟斤拷锟斤拷锟斤拷锟捷碉拷末尾锟斤拷锟斤拷锟斤拷a, A, h, H锟斤拷式锟斤拷锟斤拷锟诫,锟斤拷锟斤拷锟截革拷锟斤拷锟斤拷指锟斤拷锟剿革拷锟斤拷锟斤拷锟捷斤拷锟结被使锟矫硷拷锟斤拷锟街凤拷锟斤拷锟斤拷锟斤拷锟斤拷@,锟斤拷锟斤拷锟斤拷锟街憋拷示锟斤拷锟斤拷剩锟斤拷锟斤拷锟捷的撅拷锟皆讹拷位锟斤拷之前锟斤拷锟斤拷锟捷斤拷锟结被锟斤拷锟街凤拷锟斤拷锟斤拷洌╋拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷荩锟斤拷馗锟斤拷锟斤拷锟街革拷锟斤拷锟斤拷亩锟斤拷锟斤拷锟斤拷莶锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷傻亩锟斤拷锟斤拷锟斤拷址锟斤拷锟斤拷小锟�
目前锟斤拷实锟街的革拷式锟斤拷锟铰o拷
锟斤拷锟斤拷 | 锟斤拷锟斤拷 |
---|---|
a | 锟斤拷NUL锟街斤拷锟斤拷锟斤拷址锟斤拷锟� |
A | 锟斤拷SPACE(锟秸革拷)锟斤拷锟斤拷址锟斤拷锟� |
h | 十锟斤拷锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷锟斤拷位锟斤拷前 |
H | 十锟斤拷锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷锟斤拷位锟斤拷前 |
c | 锟叫凤拷锟斤拷锟街凤拷 |
C | 锟睫凤拷锟斤拷锟街凤拷 |
s | 锟叫凤拷锟脚讹拷锟斤拷锟斤拷(16位锟斤拷锟斤拷锟斤拷锟街斤拷锟斤拷) |
S | 锟睫凤拷锟脚讹拷锟斤拷锟斤拷(16位锟斤拷锟斤拷锟斤拷锟街斤拷锟斤拷) |
n | 锟睫凤拷锟脚讹拷锟斤拷锟斤拷(16位锟斤拷锟斤拷锟斤拷纸锟斤拷锟�) |
v | 锟睫凤拷锟脚讹拷锟斤拷锟斤拷(16位锟斤拷小锟斤拷锟街斤拷锟斤拷) |
i | 锟叫凤拷锟斤拷锟斤拷锟斤拷(锟斤拷锟斤拷锟斤拷卮锟叫★拷纸锟斤拷锟�) |
I | 锟睫凤拷锟斤拷锟斤拷锟斤拷(锟斤拷锟斤拷锟斤拷卮锟叫★拷纸锟斤拷锟�) |
l | 锟叫凤拷锟脚筹拷锟斤拷锟斤拷(32位锟斤拷锟斤拷锟斤拷锟街斤拷锟斤拷) |
L | 锟睫凤拷锟脚筹拷锟斤拷锟斤拷(32位锟斤拷锟斤拷锟斤拷锟街斤拷锟斤拷) |
N | 锟睫凤拷锟脚筹拷锟斤拷锟斤拷(32位锟斤拷锟斤拷锟斤拷纸锟斤拷锟�) |
V | 锟睫凤拷锟脚筹拷锟斤拷锟斤拷(32位锟斤拷小锟斤拷锟街斤拷锟斤拷) |
q | 锟叫凤拷锟脚筹拷锟斤拷锟斤拷锟斤拷(64位锟斤拷锟斤拷锟斤拷锟街斤拷锟斤拷) |
Q | 锟睫凤拷锟脚筹拷锟斤拷锟斤拷锟斤拷(64位锟斤拷锟斤拷锟斤拷锟街斤拷锟斤拷) |
J | 锟睫凤拷锟脚筹拷锟斤拷锟斤拷锟斤拷(64位锟斤拷锟斤拷锟斤拷纸锟斤拷锟�) |
P | 锟睫凤拷锟脚筹拷锟斤拷锟斤拷锟斤拷(64位锟斤拷小锟斤拷锟街斤拷锟斤拷) |
f | 锟斤拷锟斤拷锟饺革拷锟斤拷锟斤拷(锟斤拷锟斤拷锟斤拷卮锟叫�) |
g | 锟斤拷锟斤拷锟饺革拷锟斤拷锟斤拷(锟斤拷锟斤拷锟斤拷卮锟叫★拷锟叫★拷锟斤拷纸锟斤拷锟�) |
G | 锟斤拷锟斤拷锟饺革拷锟斤拷锟斤拷(锟斤拷锟斤拷锟斤拷卮锟叫★拷锟斤拷锟斤拷锟街斤拷锟斤拷) |
d | 双锟斤拷锟饺革拷锟斤拷锟斤拷(锟斤拷锟斤拷锟斤拷卮锟叫�) |
e | 双锟斤拷锟饺革拷锟斤拷锟斤拷(锟斤拷锟斤拷锟斤拷卮锟叫★拷锟叫★拷锟斤拷纸锟斤拷锟�) |
E | 双锟斤拷锟饺革拷锟斤拷锟斤拷(锟斤拷锟斤拷锟斤拷卮锟叫★拷锟斤拷锟斤拷锟街斤拷锟斤拷) |
x | NUL锟街斤拷 |
X | 锟斤拷锟斤拷锟斤拷锟街斤拷 |
Z | 锟斤拷NUL锟街斤拷锟斤拷锟斤拷址锟斤拷锟斤拷瞻锟�(PHP 5.5锟斤拷锟铰硷拷锟斤拷锟�) |
@ | NUL锟斤拷涞斤拷锟斤拷锟轿伙拷锟� |
args
锟斤拷锟截帮拷锟斤拷锟斤拷锟捷的讹拷锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷
锟芥本 | 说锟斤拷 |
---|---|
7.2.0 | float 锟斤拷 double 锟斤拷锟斤拷支锟街达拷虾锟叫★拷恕锟� |
7.0.15,7.1.1 | 锟斤拷锟斤拷锟�"e","E","g"锟斤拷"G"锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷float锟斤拷double锟斤拷锟街斤拷顺锟斤拷支锟街★拷 |
5.6.3 | 锟斤拷锟斤拷锟�"q"锟斤拷"q"锟斤拷"J"锟斤拷"P"锟斤拷锟斤拷锟斤拷支锟街达拷锟斤拷64位锟斤拷锟街★拷 |
5.5.0 | "Z"锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟�"a"锟斤拷效锟侥癸拷锟杰o拷锟斤拷实锟斤拷Perl锟斤拷锟斤拷锟皆★拷 |
Example #1 pack() 锟斤拷锟斤拷
<?php
$binarydata = pack("nvc*", 0x1234, 0x5678, 65, 66);
?>
The resulting binary string will be 6 bytes long and contain the byte sequence 0x12, 0x34, 0x78, 0x56, 0x41, 0x42.
Note that PHP internally stores integer values as signed values of a machine-dependent size (C type long). Integer literals and operations that yield numbers outside the bounds of the integer type will be stored as float. 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.
The most relevant case is when packing unsigned numbers that would be representable with the integer type if it were unsigned. In systems where the integer type has a 32-bit size, the cast usually results in the same byte pattern as if the integer were unsigned (although this relies on implementation-defined unsigned to signed conversions, as per the C standard). In systems where the integer type has 64-bit size, the float 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 int type (most UNIX-like systems don't), the only way to use the I pack format in the upper range is to create integer negative values with the same byte representation as the desired unsigned value.