Open In App

PHP | unpack() Function

Last Updated : 09 Jul, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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"));
?>


Output:

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));
?>


Output:

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));
?>


Output:

array(3) {
  [1]=>
  int(56)
  [2]=>
  int(49)
  [3]=>
  int(54)
}

Reference: https://www.php.net/manual/en/function.unpack.php



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads