Rechercher une fonction PHP

unpack

(PHP 4, PHP 5, PHP 7)

unpackUnpack data from binary string

Description

unpack ( string $format , string $data [, int $offset = 0 ] ) : array

Unpacks from a binary string into an array according to the given format.

The unpacked data is stored in an associative array. To accomplish this you have to name the different format codes and separate them by a slash /. If a repeater argument is present, then each of the array keys will have a sequence number behind the given name.

PHP: unpack - Manual Home of Manuel PHP  Contents Haut

Parameters

format

See pack() for an explanation of the format codes.

data

The packed data.

offset

The offset to begin unpacking from.

PHP: unpack - Manual Home of Manuel PHP  Contents Haut

Return Values

Returns an associative array containing unpacked elements of binary string.

PHP: unpack - Manual Home of Manuel PHP  Contents Haut

Changelog

Version Description
7.2.0 float and double types supports both Big Endian and Little Endian.
7.1.0 The optional offset has been added.
5.5.0

Changes were made to bring this function into line with Perl:

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.

PHP: unpack - Manual Home of Manuel PHP  Contents Haut

Examples

Example #1 unpack() example

<?php
$binarydata 
"\x04\x00\xa0\x00";
$array unpack("cchars/nint"$binarydata);
print_r($array);
?>

The above example will output:

Array
(
    [chars] => 4
    [int] => 160
)

Example #2 unpack() example with a repeater

<?php
$binarydata 
"\x04\x00\xa0\x00";
$array unpack("c2chars/nint"$binarydata);
print_r($array);
?>

The above example will output:

Array
(
    [chars1] => 4
    [chars2] => 0
    [int] => 40960
)

PHP: unpack - Manual Home of Manuel PHP  Contents Haut

Notes

Caution

Note that PHP internally stores integral values as signed. If you unpack a large unsigned long and it is of the same size as PHP internally stored values the result will be a negative number even though unsigned unpacking was specified.

Caution

If you do not name an element, numeric indices starting from 1 are used. Be aware that if you have more than one unnamed element, some data is overwritten because the numbering restarts from 1 for each element.

Example #3 unpack() example with unnamed keys

<?php
$binarydata 
"\x32\x42\x00\xa0";
$array unpack("c2/n"$binarydata);
var_dump($array);
?>

The above example will output:

array(2) {
  [1]=>
  int(160)
  [2]=>
  int(66)
}

Note that the first value from the c specifier is overwritten by the first value from the n specifier.

PHP: unpack - Manual Home of Manuel PHP  Contents Haut

See Also

  • pack() - Pack data into binary string

Find a PHP function

English translation

You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.

If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.

Thank you in advance.

Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-unpack.html

The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.

References

  1. View the html document Language of the document:fr Manuel PHP : http://php.net

These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.

Contents Haut