pack() - php 选项信息函数
pack()
(PHP 4, PHP 5, PHP 7)
将数据打包成二进制字符串
说明
pack(string $format[,mixed $args[,mixed$...
]]): string根据$format将给地的参数打包成二进制字符串。
这个函数的思想来自Perl,所有格式化代码($format)的工作原理都与Perl相同。但是,缺少了部分格式代码,比如Perl的“u”。
注意,有符号值和无符号值之间的区别只影响函数unpack(),在那些使用有符号和无符号格式代码的地方pack()函数产生相同的结果。
参数
$format$format字符串由格式代码组成,后面跟着一个可选的重复参数。重复参数可以是一个整数值或者*值来重复到输入数据的末尾。对于a, A, h, H格式化代码,其后的重复参数指定了给定数据将会被使用几个字符串,对于@,其后的数字表示放置剩余数据的绝对定位(之前的数据将会被空字符串填充),对于其他所有内容,重复数量指定消耗多少数据并将其打包到生成的二进制字符串中。
目前已实现的格式如下:
代码 | 描述 |
---|---|
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填充到绝对位置 |
返回值
返回包含数据的二进制字符串。
更新日志
版本 | 说明 |
---|---|
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”等效的功能,以实现Perl兼容性。 |
范例
Example #1pack()范例
The resulting binary string will be 6 bytes long and contain the byte sequence 0x12, 0x34, 0x78, 0x56, 0x41, 0x42.
注释
CautionNote that PHP internally storesintegervalues as signed values of a machine-dependent size (C typelong). Integer literals and operations that yield numbers outside the bounds of theintegertype will be stored asfloat. 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 theintegertype if it were unsigned. In systems where theintegertype has a 32-bit size, the cast usually results in the same byte pattern as if theintegerwere unsigned (although this relies on implementation-defined unsigned to signed conversions, as per the C standard). In systems where theintegertype has 64-bit size,thefloatmost 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 Cinttype (most UNIX-like systems don't), the only way to use theIpack format in the upper range is to createintegernegative values with the same byte representation as the desired unsigned value.
参见
unpack()
Unpack data from binary string
If you'd like to understand pack/unpack. There is a tutorial here in perl, that works equally well in understanding it for php: http://perldoc.perl.org/perlpacktut.html
A helper class to convert integer to binary strings and vice versa. Useful for writing and reading integers to / from files or sockets. Usage example:
Note that the the upper command in perl looks like this: $binarydata = pack ("n v c*", 0x1234, 0x5678, 65, 66); In PHP it seems that no whitespaces are allowed in the first parameter. So if you want to convert your pack command from perl -> PHP, don't forget to remove the whitespaces!
If you need to unpack a signed short from big-endian or little-endian specifically, instead of machine-byte-order, you need only unpack it as the unsigned form, and then if the result is >= 2^15, subtract 2^16 from it. And example would be:
Coder's example is basically an explanation of bindec() and decbin(), not pack() and unpack(). Here's some code to convert a string binary expression into its binary-string equivalent and vice versa. (Would be even simpler if pack/unpack offered a 'b' format code....)
/* Convert float from HostOrder to Network Order */ function FToN( $val ) { $a = unpack("I",pack( "f",$val )); return pack("N",$a[1] ); } /* Convert float from Network Order to HostOrder */ function NToF($val ) { $a = unpack("N",$val); $b = unpack("f",pack( "I",$a[1])); return $b[1]; }
Even though in a 64-bit architecure intval(6123456789) = 6123456789, and sprintf('%b', 5000000000) = 100101010000001011111001000000000 pack will not treat anything passed to it as 64-bit. If you want to pack a 64-bit integer: results in: Array ( [1] => 1 [2] => 705032704 ) 5000000000 but ONLY on a 64-bit enabled machine and PHP distro.
I've spent a number of hours (n>=2) finding how to do this, it works like the c function 'ntohs', used for eg the socks5 proxy protocol.
a cool function to converrt numbers to Persian numbers(utf-8) origin: http://www.farsiweb.info/jalali/jalali.phps function farsinum($str) { $ret = ""; for ($i = 0; $i = '0' && $c or unpack('N', ...) for big-endianness.
These two functions allow conversion between binary string and signed integer with possibility to give the bit length. Usage:
pack() h Hex string, low nibble first (not same hex2bin()) H Hex string, high nibble first (same hex2bin())
Using pack to write Arabic char(s) to a file.
This is how I used pack to convert base2 to base64 since base_convert doesn't support base64 The base conversions don't work for long strings, which is why I convert 1 byte at a time Hope this helps someone function base2to64($base2) { if ($remainbits = strlen($base2)%8) $base2 .= str_repeat('0',8-$remainbits); $base64 = NULL; for ($i=0;$i"91","F8"=>"9B","E5"=>"86","C6"=>"92","D8"=>"9D", "C5"=>"8F"); for($i = 0; $iYou can use pack to strip the byte order mark (BOM) from a file. For example, strip the UTF-8 BOM: This function could be easily adjusted to match any byte order mark. Have a look at wikipedia for a full list of hex codes for each specific encoding. - GerardArray pack:How to convert an integer to raw data, specifying the amount of bytes to convert to (will be padded with 0's): public function toRaw($int, $bytes = 4) { $hex = dechex($int); if (strlen($hex) % 2 != 0) $hex = '0'. $hex; $arr = str_split($hex, 2); while (count($arr)
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!