PHP | unpack() Function - GeeksforGeeks

09 Jul, 2019
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.
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
PHP-function
PHP
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to fetch data from localserver database and display on HTML table using PHP ?
How to create admin login page using PHP?
PHP str_replace() Function
How to pass form variables from one page to other page in PHP ?
Different ways for passing data to view in Laravel
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?