Unpack data from a binary string:
<?php$data = "PHP";print_r(unpack("C*",$data));?>The unpack() function unpacks data from a binary string.
unpack(format,data)
parameter | describe |
---|---|
format | Required. Specifies the format used when unpacking data. Possible values: a - a NUL padded string A - SPACE filled string h - Hexadecimal string, low-order bit first H - Hexadecimal string, high-order bit first c - signed char C - unsigned char s - signed short (always 16 bits, machine byte order) S - unsigned short (always 16 bits, machine byte order) n - unsigned short (always 16 bits, big endian byte order) v - unsigned short (always 16 bits, little endian byte order) i - signed integer (depends on machine size and byte order) I - unsigned integer (depends on machine size and byte order) l - signed long (always 32 bits, machine byte order) L - unsigned long (always 32 bits, machine byte order) N - unsigned long (always 32 bits, big endian byte order) V - unsigned long (always 32 bits, little endian byte order) f - float (depends on machine size and representation) d - double (depends on machine size and representation) x - NUL byte X - Back up one byte Z - NUL padded string @ - NUL fills the absolute position |
data | Required. Specifies the binary data to be unpacked. |
Return value: | Returns an array if successful, or FALSE if failed. |
---|---|
PHP version: | 4+ |
Update log: | As of PHP 5.5.0, the following changes have been made for Perl compatibility: "a" code retains the trailing NULL byte. The "A" code removes all trailing ASCII whitespace. Added "Z" code for NUL padded strings and removes trailing NULL bytes. |
Unpack the data:
<?php$data = "PHP";print_r(unpack("C*myint",$data));?>Unpack the data:
<?php$bin = pack("c2n2",0x1234,0x5678,65,66);print_r(unpack("c2chars/n2int",$bin));?>