目录

PHP pack() 函数

❮ PHP 其他参考

示例

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

<?php
echo pack("C3",80,72,80);
?>
亲自试一试 »

定义和用法

pack() 函数将数据打包成二进制字符串。


语法

pack( format,args+)

参数值

Parameter Description
format Required. Specifies the format to use when packing data.

Possible values:

  • a - NUL-padded string
  • A - SPACE-padded string
  • h - Hex string, low nibble first
  • H - Hex string, high nibble first
  • c - signed char
  • C - unsigned char
  • s - signed short (always 16 bit, machine byte order)
  • S - unsigned short (always 16 bit, machine byte order)
  • n - unsigned short (always 16 bit, big endian byte order)
  • v - unsigned short (always 16 bit, little endian byte order)
  • i - signed integer (machine dependent size and byte order)
  • I - unsigned integer (machine dependent size and byte order)
  • l - signed long (always 32 bit, machine byte order)
  • L - unsigned long (always 32 bit, machine byte order)
  • N - unsigned long (always 32 bit, big endian byte order)
  • V - unsigned long (always 32 bit, little endian byte order)
  • q - signed long long (always 64 bit, machine byte order)
  • Q - unsigned long long (always 64 bit, machine byte order)
  • J - unsigned long long (always 64 bit, big endian byte order)
  • P - unsigned long long (always 64 bit, little endian byte order)
  • f - float (machine dependent size and representation)
  • g - float (machine dependent size, little endian byte order)
  • G - float (machine dependent size, big endian byte order)
  • d - double (machine dependent size and representation)
  • e - double (machine dependent size, little endian byte order)
  • E - double (machine dependent size, big endian byte order)
  • x - NUL byte
  • X - Back up one byte
  • Z - NUL-padded string
  • @ - NUL-fill to absolute position
args+ Optional. Specifies one or more arguments to be packed


技术细节

返回值: 以二进制字符串形式返回数据
PHP 版本: 4+
变更日志: PHP 7.2 - float 和 double 现在支持大端和小端。
PHP 7.0.15 - 添加了 "E"、"e"、"G"、"g" 代码。
PHP 5.6.3 - 添加了 "Q"、"q"、"J"、"P" 代码。
PHP 5.5 - 添加了 "Z" 代码(与 "a" 具有与 Perl 兼容性相同的功能)。

更多示例

示例

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

<?php
echo pack("C*",80,72,80);
?>
亲自试一试 »

❮ PHP 其他参考