Latest web development tutorials

PHP unpack () function

PHP Misc Reference Manual PHP Misc Reference Manual

Examples

From the binary string data unpack:

<?php
$data = "PHP";
print_r(unpack("C*",$data));
?>

Running instance »

Definition and Usage

unpack () function from the binary string data unpacked.


grammar

unpack(format,data)

参数 描述
format 必需。规定在解包数据时所使用的格式。

可能的值:

  • a - NUL 填充的字符串
  • A - SPACE 填充的字符串
  • h - 十六进制字符串,低位在前
  • H - 十六进制字符串,高位在前
  • c - signed char
  • C - unsigned char
  • s - signed short(总是16位, machine 字节顺序)
  • S - unsigned short(总是16位, machine 字节顺序)
  • n - unsigned short(总是16位, big endian 字节顺序)
  • v - unsigned short(总是16位, little endian 字节顺序)
  • i - signed integer(取决于 machine 的大小和字节顺序)
  • I - unsigned integer(取决于 machine 的大小和字节顺序)
  • l - signed long(总是32位, machine 字节顺序)
  • L - unsigned long(总是32位, machine 字节顺序)
  • N - unsigned long(总是32位, big endian 字节顺序)
  • V - unsigned long(总是32位, little endian 字节顺序)
  • f - float(取决于 machine 的大小和表示)
  • d - double(取决于 machine 的大小和表示)
  • x - NUL 字节
  • X - 备份一个字节
  • Z - NUL 填充的字符串
  • @ - NUL 填充绝对位置
data 必需。规定被解包的二进制数据。

technical details

return value: If successful, it returns an array, if it fails to return FALSE.
PHP version: 4+
Update log: Since PHP 5.5.0 onwards, the Perl compatible the following changes:

"A" code holding the trailing NULL byte.
"A" ASCII code removes all trailing blanks.
New "Z" code is used to fill the NUL string and remove the trailing NULL byte.


More examples

Example 1

Unpack the data:

<?php
$data = "PHP";
print_r(unpack("C*myint",$data));
?>

Running instance »

Example 2

Unpack the data:

<?php
$bin = pack("c2n2",0x1234,0x5678,65,66);
print_r(unpack("c2chars/n2int",$bin));
?>

Running instance »


PHP Misc Reference Manual PHP Misc Reference Manual