略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: pack

2024-11-23

pack

(PHP 4, PHP 5, PHP 7, PHP 8)

pack将数据打包成二进制字符串

说明

pack(string $format, mixed ...$values): string

将输入参数打包成 format 格式的二进制字符串。

这个函数的思想来自 Perl,所有格式化代码(format)的工作原理都与 Perl 相同。 但是,缺少了部分格式代码,比如 Perl 的 “u”。

注意,有符号值和无符号值之间的区别只影响函数 unpack(),在那些使用有符号和无符号格式代码的地方 pack() 函数产生相同的结果。

参数

format

format 字符串由格式代码组成,后面跟着一个可选的重复参数。重复参数可以是一个整数值或者 * 值来重复到输入数据的末尾。对于 a, A, h, H 格式化代码,其后的重复参数指定了给定数据将会被使用几个字符串。对于 @,其后的数字表示放置剩余数据的绝对定位(之前的数据将会被空字符串填充),对于其他所有内容,重复数量指定消耗多少个数据参数并将其打包到生成的二进制字符串中。

目前已实现的格式如下:

pack() 格式字符
代码 描述
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 字节填充字符串空白
@ NUL 填充到绝对位置
values

返回值

返回包含数据的二进制字符串, 或者在失败时返回 false

更新日志

版本 说明
8.0.0 此函数不再在失败时返回 false
7.2.0 floatdouble 类型支持大端和小端。
7.0.15,7.1.1 添加了 “e”,“E”,“g” 和 “G” 代码以启用 float 和 double 的字节顺序支持。

范例

示例 #1 pack() 范例

<?php
$binarydata 
pack("nvc*"0x12340x56786566);
?>

输出结果为长度为 6 字节的二进制字符串,包含以下序列 0x12, 0x34, 0x78, 0x56, 0x41, 0x42。

注释

警告

Note that PHP internally stores int values as signed values of a machine-dependent size (C type long). Integer literals and operations that yield numbers outside the bounds of the int 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 int type if it were unsigned. In systems where the int type has a 32-bit size, the cast usually results in the same byte pattern as if the int were unsigned (although this relies on implementation-defined unsigned to signed conversions, as per the C standard). In systems where the int 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 int negative values with the same byte representation as the desired unsigned value.

参见

  • unpack() - Unpack data from binary string
add a noteadd a note

User Contributed Notes 20 notes

up
75
chadm at codeangel dot org
10 years ago
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
up
35
stanislav dot eckert at vizson dot de
6 years ago
A helper class to convert integer to binary strings and vice versa. Useful for writing and reading integers to / from files or sockets.

<?php

   
class int_helper
   
{
        public static function
int8($i) {
            return
is_int($i) ? pack("c", $i) : unpack("c", $i)[1];
        }

        public static function
uInt8($i) {
            return
is_int($i) ? pack("C", $i) : unpack("C", $i)[1];
        }

        public static function
int16($i) {
            return
is_int($i) ? pack("s", $i) : unpack("s", $i)[1];
        }

        public static function
uInt16($i, $endianness=false) {
           
$f = is_int($i) ? "pack" : "unpack";

            if (
$endianness === true) {  // big-endian
               
$i = $f("n", $i);
            }
            else if (
$endianness === false) {  // little-endian
               
$i = $f("v", $i);
            }
            else if (
$endianness === null) {  // machine byte order
               
$i = $f("S", $i);
            }

            return
is_array($i) ? $i[1] : $i;
        }

        public static function
int32($i) {
            return
is_int($i) ? pack("l", $i) : unpack("l", $i)[1];
        }

        public static function
uInt32($i, $endianness=false) {
           
$f = is_int($i) ? "pack" : "unpack";

            if (
$endianness === true) {  // big-endian
               
$i = $f("N", $i);
            }
            else if (
$endianness === false) {  // little-endian
               
$i = $f("V", $i);
            }
            else if (
$endianness === null) {  // machine byte order
               
$i = $f("L", $i);
            }

            return
is_array($i) ? $i[1] : $i;
        }

        public static function
int64($i) {
            return
is_int($i) ? pack("q", $i) : unpack("q", $i)[1];
        }

        public static function
uInt64($i, $endianness=false) {
           
$f = is_int($i) ? "pack" : "unpack";

            if (
$endianness === true) {  // big-endian
               
$i = $f("J", $i);
            }
            else if (
$endianness === false) {  // little-endian
               
$i = $f("P", $i);
            }
            else if (
$endianness === null) {  // machine byte order
               
$i = $f("Q", $i);
            }

            return
is_array($i) ? $i[1] : $i;
        }
    }
?>

Usage example:
<?php
    Header
("Content-Type: text/plain");
    include(
"int_helper.php");

    echo
int_helper::uInt8(0x6b) . PHP_EOL// k
   
echo int_helper::uInt8(107) . PHP_EOL// k
   
echo int_helper::uInt8("\x6b") . PHP_EOL . PHP_EOL// 107

   
echo int_helper::uInt16(4101) . PHP_EOL// \x05\x10
   
echo int_helper::uInt16("\x05\x10") . PHP_EOL// 4101
   
echo int_helper::uInt16("\x05\x10", true) . PHP_EOL . PHP_EOL// 1296

   
echo int_helper::uInt32(2147483647) . PHP_EOL// \xff\xff\xff\x7f
   
echo int_helper::uInt32("\xff\xff\xff\x7f") . PHP_EOL . PHP_EOL// 2147483647

    // Note: Test this with 64-bit build of PHP
   
echo int_helper::uInt64(9223372036854775807) . PHP_EOL// \xff\xff\xff\xff\xff\xff\xff\x7f
   
echo int_helper::uInt64("\xff\xff\xff\xff\xff\xff\xff\x7f") . PHP_EOL . PHP_EOL// 9223372036854775807

?>
up
18
plutus at gmx dot de
21 years ago
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!
up
13
FrozenFire
11 years ago
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:

<?php
$foo
= unpack("n", $signedbigendianshort);
$foo = $foo[1];
if(
$foo >= pow(2, 15)) $foo -= pow(2, 16);
?>
up
6
j.s.hoekstra
16 years ago
/* 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];
}
up
8
Anonymous
12 years ago
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....)

<?php
function bin2bstr($input)
// Convert a binary expression (e.g., "100111") into a binary-string
{
  if (!
is_string($input)) return null; // Sanity check

  // Pack into a string
 
return pack('H*', base_convert($input, 2, 16));
}

function
bstr2bin($input)
// Binary representation of a binary-string
{
  if (!
is_string($input)) return null; // Sanity check

  // Unpack as a hexadecimal string
 
$value = unpack('H*', $input);
 
 
// Output binary representation
 
return base_convert($value[1], 16, 2);
}

// Returns string(3) "ABC"
var_dump(bin2bstr('01000001 01000010 01000011'));

// Returns string(24) "010000010100001001000011"
var_dump(bstr2bin('ABC'));
?>
up
4
php at nagler-ihlein dot de
14 years ago
Be aware of format code H always padding the 0 for byte-alignment to the right (for odd count of nibbles).

So pack("H", "7") results in 0x70 (ASCII character 'p') and not in 0x07 (BELL character)
as well as pack("H*", "347") results in 0x34 ('4') and 0x70 ('p') and not 0x03 and 0x47.
up
4
zilinex at yahoo dot com
17 years ago
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 < strlen($str); ++$i) {
        $c = $str[$i];
        if( $c >= '0' && $c <= '9' )
                $out .= pack("C*", 0xDB, 0xB0 + $c);
        else
                $ret .= $c;
  }
  return $ret;
}
up
3
Quis AT spam.to.my.devnull.quis.cx
14 years ago
<?PHP
function ntohs($port) {
 
$b=pack("N", $port);
  return
substr($b,2,2);
}
?>

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.
up
3
petepostma at gmail dot spam dot com
9 years ago
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:

<?php
$big
= 5000000000;

$left = 0xffffffff00000000;
$right = 0x00000000ffffffff;

$l = ($big & $left) >>32;
$r = $big & $right;

$good = pack('NN', $l, $r);

$urlsafe = str_replace(array('+','/'), array('-','_'), base64_encode($good));

//done!

//rebuild:
$unurl str_replace(array('-','_'), array('+','/'), $urlsafe);
$binary = base64_decode($unurl);

$set = unpack('N2', $tmp);
print_r($set);

$original = $set[1] << 32 | $set[2];
echo
$original, "\\r\\n";
?>

results in:
Array
(
    [1] => 1
    [2] => 705032704
)
5000000000

but ONLY on a 64-bit enabled machine and PHP distro.
up
2
Patrik Fimml
16 years ago
You will get the same effect with

<?php
function _readInt($fp)
{
   return
unpack('V', fread($fp, 4));
}
?>

or unpack('N', ...) for big-endianness.
up
1
dylan at pow7 dot com
14 years ago
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<strlen($base2);$i+=8) $base16 .= sprintf('%02x',bindec(sprintf('%08d',substr($base2,$i,8))));
    return base64_encode(pack('H*',$base16));
}
function base64to2($base64) {
    list($base16) = unpack('H*0',base64_decode($base64));
    $base2 = NULL;
    for ($i=0;$i<strlen($base16);$i++) $base2 .= sprintf('%04d',base_convert(substr($base16,$i,1),16,2));
    return $base2;
}
up
1
ru
4 years ago
pack()
h    Hex string, low nibble first (not same hex2bin())
H    Hex string, high nibble first (same hex2bin())
up
1
Coder
13 years ago
These two functions allow conversion between binary string and signed integer with possibility to give the bit length.

Usage:
<?php
echo si2bin(-10, 32);
11111111111111111111111111110110
echo si2bin(10, 32);
00000000000000000000000000001010
echo bin2si("11111111111111111111111111110110", 32);
-
10
echo bin2si("00000000000000000000000000001010", 32);
10

// signed integer to binary
function si2bin($si, $bits=32)
{
    if (
$si >= -pow(2,$bits-1) and $si <= pow(2,$bits-1) )
    {
        if (
$si >= 0) // positive or zero
       
{
           
$bin = base_convert($si,10,2);
           
// pad to $bits bit
           
$bin_length = strlen($bin);
            if (
$bin_length < $bits) $bin = str_repeat ( "0", $bits-$bin_length).$bin;
        }
        else
// negative
       
{
           
$si = -$si-pow(2,$bits);
           
$bin = base_convert($si,10,2);
           
$bin_length = strlen($bin);
            if (
$bin_length > $bits) $bin = str_repeat ( "1", $bits-$bin_length).$bin;
        }
        return
$bin;
    }
}

// binary to signed integer
function bin2si($bin,$bits=32)
{
    if (
strlen($bin)==$bits)
    {
        if (
substr($bin,0,1) == 0) // positive or zero
       
{
           
$si = base_convert($bin,2,10);
        }
        else
// negative
       
{
           
$si = base_convert($bin,2,10);
           
$si = -(pow(2,$bits)-$si);
        }
        return
$si;
    }
}
?>
up
1
Ammar Hameed
12 years ago
Using pack to write Arabic char(s) to a file.

<?php
$text
= "&#13574;&#13830;&#13830;";

$text = mb_convert_encoding($text, "UCS-2BE", "HTML-ENTITIES");

$len mb_strlen($text);

$bom = mb_convert_encoding("&#65534;", "unicode", "HTML-ENTITIES");

$fp = fopen('text.txt', 'w');

fwrite($fp, pack('a2', $bom)); 
fwrite($fp, pack("a{$len}", $text));
fwrite($fp, pack('a2', $bom));
fwrite($fp, pack('a2', "\n"));

fclose($fp);
?>
up
-2
Gerjoo at gmail dot com
11 years ago
You can use pack to strip the byte order mark (BOM) from a file.

For example, strip the UTF-8 BOM:

<?php

// Strips the UTF-8 mark: (hex value: EF BB BF)
function trimUTF8BOM($data){
    if(
substr($data, 0, 3) == pack('CCC', 239, 187, 191)) {
        return
substr($data, 3);
    }
    return
$data;
}

?>

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.

- Gerard
up
-1
SixThreeOh
12 years ago
If you're bugged by http://bugs.php.net/bug.php?id=5889 then you can try this:

Use igbinary-serialize if you don't mind a little overhead.

Or intarray (by the same person) if you don't mind using a slightly experimental package which may have problems sharing data between differently byte/bit ordered architectures.

I don't believe it would be too difficult to shove a serialize function and unserialize function in there if you rip out the code from igbinary for storing numeric arrays with the correct endianess. Looking at `igbinary_serialize32` and `igbinary_unserialize32` in igbinary.c it should be very easy to copy that functionality to intarray.c.

Take away the "<<0" though, that's just stupid :P

Ref: http://opensource.dynamoid.com/
up
-1
Newdawn.dk
16 years ago
When trying to create a ZIP file using the pack function - I experienced trouble with the "a" code - It converted all chars correct from the std. ASCII charset but not more language specific like ÆøÅ.
It seems that ZIP files do not use the same HEX for these as everything else does.
The fix was a quick workaround but you'll probably get the picture:
function UniHex($str) {
    // æ ø å Æ Ø Å
    //These are simply one HEX code being replaced by another to correct the issue
    $except = array("E6"=>"91","F8"=>"9B","E5"=>"86","C6"=>"92","D8"=>"9D",    "C5"=>"8F");
    for($i = 0; $i < strlen($str); $i++) {
        $hex = bin2hex(substr($str, $i, 1));
        if ($except[strtoupper($hex)])
            $hex = $except[strtoupper($hex)];
        $return .= $hex;
    }
    return $return;
}
And then i replaced an "a100" code with "H".strlen(uniHex($mystring))

This is like i said a quick workaround, but if you find the real reason for this i'd be happy to see it
up
-4
jrm456 at speed dot 1s dot fr
8 years ago
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) < $bytes)
            array_unshift($arr, 0);
       
        $arr = array_map('hexdec', $arr);
        $arr = array_map('chr', $arr);

        return join('', array_reverse($arr));
    }
up
-9
Anonymous
13 years ago
Array pack:
<?php
function pack_array($v,$a) {
return
call_user_func_array(pack,array_merge(array($v),(array)$a));
}
?>

官方地址:https://www.php.net/manual/en/function.pack.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3