PHP unpack() function

Definition and Usage

The unpack() function unpacks data from a binary string.

Syntax

unpack(format,data)
Parameter Description
format Required. Specifies the format used for unpacking data.
data Optional. Specifies the unpacked binary data.

Possible values for format parameter:

  • 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)
  • f - float (machine dependent size and representation)
  • d - double (machine dependent size and representation)
  • x - NULL byte
  • X - Back up one byte
  • @ - NULL fill to absolute position

مثال

مثال 1

<?php
$دیتا = "PHP";
پرنٹ_آر(آپکک("C*",$دیتا));
?>

خروج:

آرری
(
[1] => 80
[2] => 72
[3] => 80
)

مثال 2

<?php
$دیتا = "PHP";
پرنٹ_آر(آپکک("C*myint",$دیتا));
?>

خروج:

آرری
(
[myint1] => 80
[myint2] => 72
[myint3] => 80
)

مثال 3

<?php
$بین = پیکک("c2ن2",0x1234,0x5678,65,66);
پرنٹ_آر(آپکک("c2چرز/n2آئن",$بین));
?>

خروج:

آرری
(
[chars1] => 52
[chars2] => 120
[int1] => 65
[int2] => 66
)