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:
|
string |
Required. Specify the packed data. |
offset |
Optional. Specify the offset to begin unpacking from. Default is 0. |
unpack() format characters
Code | Description |
---|---|
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 |
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