PHP | unpack() Function
The unpack() function is an inbuilt function in PHP which is used to unpack from a binary string into the respective format.
Syntax:
array unpack( $format, $data, $offset )
Parameters: This function accepts three parameters as mentioned above and described below:
-
$format: It is required parameter. It specifies the format to be used while packing data.
- a – denotes string which is NUL-padded.
- A – denotes string which is SPACE-padded.
- h – denotes low nibble first Hex string.
- H – denotes high nibble first Hex string.
- c – denotes signed character.
- C – denotes unsigned character.
- s – denotes signed short (16 bit, machine byte order).
- S – denotes unsigned short (16 bit, machine byte order).
- n – denotes unsigned short (16 bit, big endian byte order).
- v – denotes unsigned short (16 bit, little endian byte order).
- i – denotes signed integer (machine dependent byte order and size).
- I – denotes unsigned integer (machine dependent byte order and size).
- l – denotes signed long (32 bit, machine byte order).
- L – denotes unsigned long (32 bit, machine byte order).
- N – denotes unsigned long (32 bit, big endian byte order).
- V – denotes unsigned long (32 bit, little endian byte order).
- f – denotes float (machine dependent representation and size).
- d – denotes double (machine dependent representation and size).
- x – denotes NUL byte.
- X – denotes Back up one byte.
- Z – denotes string which is NUL-padded.
- @ – denotes NUL-fill to absolute position.
- $data: It is Required parameter. It specifies the binary data to be unpacked.
- offset: This parameter holds the offset to begin from unpacking.
Return Value: It returns an associative array containing unpacked elements on success, or returns FALSE on failure.
Note: This function is available for PHP 4.0.0 and newer version.
Example 1: This program uses C format to unpack the data from binary string.
<?php var_dump( unpack("C*", "GEEKSFORGEEKS")); ?> |
array(13) {
[1]=>
int(71)
[2]=>
int(69)
[3]=>
int(69)
[4]=>
int(75)
[5]=>
int(83)
[6]=>
int(70)
[7]=>
int(79)
[8]=>
int(82)
[9]=>
int(71)
[10]=>
int(69)
[11]=>
int(69)
[12]=>
int(75)
[13]=>
int(83)
}
Example 2:
<?php $binary_data = pack("c2n2", 0x1634, 0x3623, 65, 66); var_dump(unpack("c2chars/n2int", $binary_data)); ?> |
array(4) {
["chars1"]=>
int(52)
["chars2"]=>
int(35)
["int1"]=>
int(65)
["int2"]=>
int(66)
}
Example 3: This example uses i format to unpack the data from binary string.
<?php $binary_data = pack("i3", 56, 49, 54); var_dump(unpack("i3", $binary_data)); ?> |
array(3) {
[1]=>
int(56)
[2]=>
int(49)
[3]=>
int(54)
}
Reference: https://www.php.net/manual/en/function.unpack.php
Recommended Posts:
- How to unpack array elements into separate variables using JavaScript ?
- How to call a function that return another function in JavaScript ?
- How to get the function name inside a function in PHP ?
- How to get the function name from within that function using JavaScript ?
- D3.js | d3.max() function
- PHP | Ds\Set add() Function
- PHP | Ds\Map xor() Function
- PHP | each() Function
- PHP | Ds\Map put() Function
- PHP | Ds\Set xor() Function
- PHP | pow( ) Function
- PHP | key() Function
- CSS | rgb() Function
- PHP | min( ) Function
- PHP | pos() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




