PHP Function Reference

PHP unpack() Function



The PHP unpack() function unpacks data from a binary string into an array according to the given format.

The unpacked data is stored in an associative array. To accomplish this the different format codes should be named and separated by a slash /. If a repeater argument is present, then each of the array keys will have a sequence number behind the given name.

Changes were made to bring this function into line with Perl which are mentioned below:

  • The "a" code now retains trailing NULL bytes.
  • The "A" code now strips all trailing ASCII whitespace (spaces, tabs, newlines, carriage returns, and NULL bytes).
  • The "Z" code was added for NULL-padded strings, and removes trailing NULL bytes.

Syntax

unpack(format, string, offset)

Parameters

format Required. Specify the format string consists of format codes followed by an optional repeater argument. The repeater argument can be either an integer value or * for repeating to the end of the input data. The repeat count specifies the following:
  • For a, A, h, H it specifies how many characters of one data argument are taken
  • For @ it is the absolute position where to put the next data,
  • For everything else it specifies how many data arguments are consumed and packed into the resulting binary string.
The currently implemented formats are tabulated below:
string Required. Specify the packed data.
offset Optional. Specify the offset to begin unpacking from. Default is 0.

unpack() format characters

CodeDescription
aNUL-padded string
ASPACE-padded string
hHex string, low nibble first
HHex string, high nibble first
csigned char
Cunsigned char
ssigned short (always 16 bit, machine byte order)
Sunsigned short (always 16 bit, machine byte order)
nunsigned short (always 16 bit, big endian byte order)
vunsigned short (always 16 bit, little endian byte order)
isigned integer (machine dependent size and byte order)
Iunsigned integer (machine dependent size and byte order)
lsigned long (always 32 bit, machine byte order)
Lunsigned long (always 32 bit, machine byte order)
Nunsigned long (always 32 bit, big endian byte order)
Vunsigned long (always 32 bit, little endian byte order)
qsigned long long (always 64 bit, machine byte order)
Qunsigned long long (always 64 bit, machine byte order)
Junsigned long long (always 64 bit, big endian byte order)
Punsigned long long (always 64 bit, little endian byte order)
ffloat (machine dependent size and representation)
gfloat (machine dependent size, little endian byte order)
Gfloat (machine dependent size, big endian byte order)
ddouble (machine dependent size and representation)
edouble (machine dependent size, little endian byte order)
Edouble (machine dependent size, big endian byte order)
xNUL byte
XBack up one byte
ZNUL-padded string
@NUL-fill to absolute position

Return Value

Returns an associative array containing unpacked elements of binary string, or false on failure.

Example: unpack() example

The example below shows the usage of unpack() function.

<?php
$binarydata = "\x04\x00\xa0\x00";
$array = unpack("cChars/nInt", $binarydata);
print_r($array);
?>

The output of the above code will be:

Array
(
    [Chars] => 4
    [Int] => 160
)

Example: unpack() example with a repeater

In the example below, the format argument contains repeater.

<?php
$binarydata = "\x04\x00\xa0\x00";
$array = unpack("c2Chars/nInt", $binarydata);
print_r($array);
?>

The output of the above code will be:

Array
(
    [Chars1] => 4
    [Chars2] => 0
    [Int] => 40960
)

Example: unpack() example with unnamed keys

If the element is not named, numeric indices starting from 1 are used as shown in the example below:

<?php
$binarydata = "\x32\x42\x00\xa0";
$array = unpack("c2/n", $binarydata);
print_r($array);
?>

The output of the above code will be:

Array
(
    [1] => 160
    [2] => 66
)

❮ PHP Miscellaneous Reference