Syntax:array unpack ($format, $data, $offset)
Parameters:This function takes three parameters as above and described below:
- $format:Required. Specifies the format to use when packing data.
- a - denotes a NUL-padded string.
- A - denotes a string with a space.
- h - denotes the low half of the first hexadecimal line.
- H - denotes the first high-cool hex string.
- with - denotes a signed character.
- C - denotes an unsigned character.
- s - denotes signed short (16 bit machine byte order).
- S - denotes an unsigned short (16 bit, machine byte order).
- n - denotes an unsigned short (16 bit, big endian byte order).
- v - denotes unsigned short (16-bit big endian byte order).
- i - denotes a signed integer (machine dependent byte order and size).
- I - denotes an unsigned integer (machine dependent byte order and size).
- l - denotes a signed long character (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 big endian byte sequence).
- f - stands for float (machine-dependent representation and size).
- d - stands for double (machine-dependent representation and size).
- x - denotes NUL bytes.
- X - denotes one byte backup.
- Z - denotes a NUL-padded string.
- @ - indicates NUL padding to an absolute position.
- $data:Required. This specifies the binary data to be decompressed.
- offset:This parameter contains the offset that starts with unpacking.
Returned value:Returns an associative array containing unpacked elements on success, or FALSE on failure.
Note.This function is available for PHP 4.0.0 and later later versions.
Example 1:This program uses the C format to decompress data from a binary string.
var_dump (unpack ( "C *"
,
"GEEKSFORGEEKS"
));
?>
Exit: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:
$binary_data
= pack (
"c2n2"
, 0x1634, 0x3623, 65, 66);
var_dump (unpack (
"c2chars / n2int"
,
$binary_data
));
?>
Exit:array (4 ) {["chars1"] = > int (52) ["chars2"] = > int (35) ["int1"] = > int (65) ["int2"] = > int (66)}
Example 3: Inthis example uses the i format to decompress data from a binary string.
$binary_data
= pack (
"i3"
, 56, 49, 54);
var_dump (unpack (
"i3"
,
$binary_data
));
?>
Exit:array (3 ) {[1] = > int (56) [2] = > int (49) [3] = > int (54)}
Help: https://www.php.net/manual/en/function.unpack.php