pack

php128 apg


PACK data into binary string.





This function returns a binary string containing data, or FALSE on failure.



<?php

str pack 
str $format mix ...$values )


where,

$format This parameter consists of format codes 
                           followed by an optional repeater argument
                           
(SEE the below TABLE)
                           
$values Values to be packed

?>
 

$format


The string that consists of format codes followed by an optional repeater argument.



FORMAT CHARACTERS
Code Description
a NUL-padded string
A SPACE-padded string
h Hex string, low nibble first
H Hex string, high nibble first
csigned char
C unsigned char
s signed short (always 16 bit, machine byte order)
S unsigned short (always 16 bit, machine byte order)
n unsigned short (always 16 bit, big endian byte order)
v unsigned short (always 16 bit, little endian byte order)
i signed integer (machine dependent size and byte order)
I unsigned integer (machine dependent size and byte order)
l signed long (always 32 bit, machine byte order)
L unsigned long (always 32 bit, machine byte order)
N unsigned long (always 32 bit, big endian byte order)
V unsigned long (always 32 bit, little endian byte order)
q signed long long (always 64 bit, machine byte order)
Q unsigned long long (always 64 bit, machine byte order)
J unsigned long long (always 64 bit, big endian byte order)
P unsigned long long (always 64 bit, little endian byte order)
f float (machine dependent size and representation)
g float (machine dependent size, little endian byte order)
G float (machine dependent size, big endian byte order)
d double (machine dependent size and representation)
e double (machine dependent size, little endian byte order)
E double (machine dependent size, big endian byte order)
x NUL byte
X Back up one byte
Z NUL-padded string (new in PHP 5.5)
@ NUL-fill to absolute position
ed48

$values


The values to be packed.



unpack

php128 apg


UNPACK data from a binary string.





This function unpacks from a binary string into an array according to the given $format.

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

arr
|false unpack str $format str $string [, int $offset ] )


where,

$format This parameter consists of format codes 
                           followed by an optional repeater argument
                           
(SEE the below TABLE)
                           
$string The pached data
                           
$offset 
The offset to begin unpacking from

?>
 

$format


The string that consists of format codes followed by an optional repeater argument.



FORMAT CHARACTERS
Code Description
a NUL-padded string
A SPACE-padded string
h Hex string, low nibble first
H Hex string, high nibble first
csigned char
C unsigned char
s signed short (always 16 bit, machine byte order)
S unsigned short (always 16 bit, machine byte order)
n unsigned short (always 16 bit, big endian byte order)
v unsigned short (always 16 bit, little endian byte order)
i signed integer (machine dependent size and byte order)
I unsigned integer (machine dependent size and byte order)
l signed long (always 32 bit, machine byte order)
L unsigned long (always 32 bit, machine byte order)
N unsigned long (always 32 bit, big endian byte order)
V unsigned long (always 32 bit, little endian byte order)
q signed long long (always 64 bit, machine byte order)
Q unsigned long long (always 64 bit, machine byte order)
J unsigned long long (always 64 bit, big endian byte order)
P unsigned long long (always 64 bit, little endian byte order)
f float (machine dependent size and representation)
g float (machine dependent size, little endian byte order)
G float (machine dependent size, big endian byte order)
d double (machine dependent size and representation)
e double (machine dependent size, little endian byte order)
E double (machine dependent size, big endian byte order)
x NUL byte
X Back up one byte
Z NUL-padded string (new in PHP 5.5)
@ NUL-fill to absolute position
ed48

$string


The packed data.



$offset


The offset to begin unpacking from.



  1 EXERCISE   

<?php
if (PHP_INT_SIZE 8) {
    die(
"skip 64bit test only");
}

print_r(unpack("Q"pack("Q"0xfffffffffffe)));
echo 
'<br><br>';
print_r(unpack("Q"pack("Q"0)));
echo 
'<br><br>'
print_r(unpack("Q"pack("Q"0x8000000000000002)));
echo 
'<br><br>'
print_r(unpack("Q"pack("Q", -1)));
echo 
'<br><br>'
print_r(unpack("Q"pack("Q"0x8000000000000000)));

echo 
'<br><br>'
print_r(unpack("J"pack("J"0xfffffffffffe)));
echo 
'<br><br>'
print_r(unpack("J"pack("J"0)));
echo 
'<br><br>'
print_r(unpack("J"pack("J"0x8000000000000002)));
echo 
'<br><br>'
print_r(unpack("J"pack("J", -1)));
echo 
'<br><br>'
print_r(unpack("J"pack("J"0x8000000000000000)));

echo 
'<br><br>'
print_r(unpack("P"pack("P"0xfffffffffffe)));
echo 
'<br><br>'
print_r(unpack("P"pack("P"0)));
echo 
'<br><br>'
print_r(unpack("P"pack("P"0x8000000000000002)));
echo 
'<br><br>'
print_r(unpack("P"pack("P", -1)));
echo 
'<br><br>'
print_r(unpack("P"pack("P"0x8000000000000000)));

echo 
'<br><br>'
print_r(unpack("q"pack("q"0xfffffffffffe)));
echo 
'<br><br>'
print_r(unpack("q"pack("q"0)));
echo 
'<br><br>'
print_r(unpack("q"pack("q"0x8000000000000002)));
echo 
'<br><br>'
print_r(unpack("q"pack("q", -1)));
echo 
'<br><br>'
print_r(unpack("q"pack("q"0x8000000000000000)));
echo 
'<br><br><br><br>';

if (
PHP_INT_MAX 2147483647) {
    die(
"skip 32bit test only");
}

echo 
'<br><br>'
print_r(unpack("A"pack("A""hello world")));
echo 
'<br><br>'
print_r(unpack("A*"pack("A*""hello world")));
echo 
'"'.pack("A9""hello").'"';
echo 
"<br>";

echo 
'<br><br>'
print_r(unpack("C"pack("C", -127)));
echo 
'<br><br>'
print_r(unpack("C"pack("C"127)));
echo 
'<br><br>'
print_r(unpack("C"pack("C"255)));
echo 
'<br><br>'
print_r(unpack("C"pack("C", -129)));

echo 
'<br><br>'
print_r(unpack("H"pack("H"0x04)));

echo 
'<br><br>'
print_r(unpack("I"pack("I"65534)));
echo 
'<br><br>'
print_r(unpack("I"pack("I"0)));
echo 
'<br><br>'
print_r(unpack("I"pack("I", -1000)));
echo 
'<br><br>'
print_r(unpack("I"pack("I", -64434)));
echo 
'<br><br>'
print_r(unpack("I"pack("I"4294967296)));
echo 
'<br><br>'
print_r(unpack("I"pack("I", -4294967296)));

echo 
'<br><br>'
print_r(unpack("L"pack("L"65534)));
echo 
'<br><br>'
print_r(unpack("L"pack("L"0)));
echo 
'<br><br>'
print_r(unpack("L"pack("L"2147483650)));
echo 
'<br><br>'
print_r(unpack("L"pack("L"4294967295)));
echo 
'<br><br>'
print_r(unpack("L"pack("L", -2147483648)));

echo 
'<br><br>'
print_r(unpack("N"pack("N"65534)));
echo 
'<br><br>'
print_r(unpack("N"pack("N"0)));
echo 
'<br><br>'
print_r(unpack("N"pack("N"2147483650)));
echo 
'<br><br>'
print_r(unpack("N"pack("N"4294967296)));
echo 
'<br><br>'
print_r(unpack("N"pack("N", -2147483648)));
echo 
'<br><br>'
print_r(unpack("N"pack("N", -30000)));

echo 
'<br><br>'
print_r(unpack("S"pack("S"65534)));
echo 
'<br><br>'
print_r(unpack("S"pack("S"65537)));
echo 
'<br><br>'
print_r(unpack("S"pack("S"0)));
echo 
'<br><br>'
print_r(unpack("S"pack("S", -1000)));
echo 
'<br><br>'
print_r(unpack("S"pack("S", -64434)));
echo 
'<br><br>'
print_r(unpack("S"pack("S", -65535)));

echo 
'<br><br>'
print_r(unpack("V"pack("V"65534)));
echo 
'<br><br>'
print_r(unpack("V"pack("V"0)));
echo 
'<br><br>'
print_r(unpack("V"pack("V"2147483650)));
echo 
'<br><br>'
print_r(unpack("V"pack("V"4294967296)));
echo 
'<br><br>'
print_r(unpack("V"pack("V", -2147483648)));

echo 
'<br><br>'
print_r(unpack("a"pack("a""hello world")));
echo 
'<br><br>'
print_r(unpack("a*"pack("a*""hello world")));

echo 
'<br><br>'
print_r(unpack("c"pack("c", -127)));
echo 
'<br><br>'
print_r(unpack("c"pack("c"127)));
echo 
'<br><br>'
print_r(unpack("c"pack("c"255)));
echo 
'<br><br>'
print_r(unpack("c"pack("c", -129)));

echo 
'<br><br>'
print_r(unpack("h"pack("h"3000000)));

echo 
'<br><br>'
print_r(unpack("i"pack("i"65534)));
echo 
'<br><br>'
print_r(unpack("i"pack("i"0)));
echo 
'<br><br>'
print_r(unpack("i"pack("i", -1000)));
echo 
'<br><br>'
print_r(unpack("i"pack("i", -64434)));
echo 
'<br><br>'
print_r(unpack("i"pack("i", -65535)));
echo 
'<br><br>'
print_r(unpack("i"pack("i", -2147483647)));

echo 
'<br><br>'
print_r(unpack("l"pack("l"65534)));
echo 
'<br><br>'
print_r(unpack("l"pack("l"0)));
echo 
'<br><br>'
print_r(unpack("l"pack("l"2147483650)));
echo 
'<br><br>'
print_r(unpack("l"pack("l"4294967296)));
echo 
'<br><br>'
print_r(unpack("l"pack("l", -2147483648)));

echo 
'<br><br>'
print_r(unpack("n"pack("n"65534)));
echo 
'<br><br>'
print_r(unpack("n"pack("n"65537)));
echo 
'<br><br>'
print_r(unpack("n"pack("n"0)));
echo 
'<br><br>'
print_r(unpack("n"pack("n", -1000)));
echo 
'<br><br>'
print_r(unpack("n"pack("n", -64434)));
echo 
'<br><br>'
print_r(unpack("n"pack("n", -65535)));

echo 
'<br><br>'
print_r(unpack("s"pack("s"32767)));
echo 
'<br><br>'
print_r(unpack("s"pack("s"65535)));
echo 
'<br><br>'
print_r(unpack("s"pack("s"0)));
echo 
'<br><br>'
print_r(unpack("s"pack("s", -1000)));
echo 
'<br><br>'
print_r(unpack("s"pack("s", -64434)));
echo 
'<br><br>'
print_r(unpack("s"pack("s", -65535)));

echo 
'<br><br>'
print_r(unpack("v"pack("v"65534)));
echo 
'<br><br>'
print_r(unpack("v"pack("v"65537)));
echo 
'<br><br>'
print_r(unpack("v"pack("v"0)));
echo 
'<br><br>'
print_r(unpack("v"pack("v", -1000)));
echo 
'<br><br>'
print_r(unpack("v"pack("v", -64434)));
echo 
'<br><br>'
print_r(unpack("v"pack("v", -65535)));

?>

  2 EXERCISE   

<?php

var_dump
(
    
pack("A5""foo "),
    
pack("A4""fooo"),
    
pack("A4""foo"),
    
unpack("A*""foo\0\rbar\0 \t\r\n"),
    
unpack("A4""foo\0\rbar\0 \t\r\n")
);

?>

  3 EXERCISE   

<?php

var_dump
(
'pack e',
bin2hex(pack("e""")),
bin2hex(pack("e""a")),
bin2hex(pack("e"" ")),
bin2hex(pack("e"NULL)),
bin2hex(pack("e"0)),
bin2hex(pack("e"1)),
bin2hex(pack("e"1.0)),
bin2hex(pack("e"10000000000000000)),
bin2hex(pack("e"0.591234709823149)),
bin2hex(pack("e"12345678901234567890.1234567898765432123456789)),
bin2hex(pack("e", -1)),
bin2hex(pack("e", -1.0)),
bin2hex(pack("e", -10000000000000000)),
bin2hex(pack("e", -0.591234709823149)),
bin2hex(pack("e", -12345678901234567890.1234567898765432123456789)) );
echo 
'<br><br>';

var_dump(
'pack E',
bin2hex(pack("E""")),
bin2hex(pack("E""a")),
bin2hex(pack("E"" ")),
bin2hex(pack("E"NULL)),
bin2hex(pack("E"0)),
bin2hex(pack("E"1)),
bin2hex(pack("E"1.0)),
bin2hex(pack("E"10000000000000000)),
bin2hex(pack("E"0.591234709823149)),
bin2hex(pack("E"12345678901234567890.1234567898765432123456789)),
bin2hex(pack("E", -1)),
bin2hex(pack("E", -1.0)),
bin2hex(pack("E", -10000000000000000)),
bin2hex(pack("E", -0.591234709823149)),
bin2hex(pack("E", -12345678901234567890.1234567898765432123456789)) );
echo 
'<br><br>';

var_dump(
'pack g',
bin2hex(pack("g""")),
bin2hex(pack("g""a")),
bin2hex(pack("g"" ")),
bin2hex(pack("g"NULL)),
bin2hex(pack("g"0)),
bin2hex(pack("g"1)),
bin2hex(pack("g"1.0)),
bin2hex(pack("g"10000000000000000)),
bin2hex(pack("g"0.591234709823149)),
bin2hex(pack("g"12345678901234567890.1234567898765432123456789)),
bin2hex(pack("g", -1)),
bin2hex(pack("g", -1.0)),
bin2hex(pack("g", -10000000000000000)),
bin2hex(pack("g", -0.591234709823149)),
bin2hex(pack("g", -12345678901234567890.1234567898765432123456789)) );
echo 
'<br><br>';

var_dump(
'pack G',
bin2hex(pack("G""")),
bin2hex(pack("G""a")),
bin2hex(pack("G"" ")),
bin2hex(pack("G"NULL)),
bin2hex(pack("G"0)),
bin2hex(pack("G"1)),
bin2hex(pack("G"1.0)),
bin2hex(pack("G"10000000000000000)),
bin2hex(pack("G"0.591234709823149)),
bin2hex(pack("G"12345678901234567890.1234567898765432123456789)),
bin2hex(pack("G", -1)),
bin2hex(pack("G", -1.0)),
bin2hex(pack("G", -10000000000000000)),
bin2hex(pack("G", -0.591234709823149)),
bin2hex(pack("G", -12345678901234567890.1234567898765432123456789)) );
echo 
'<br><br>';

var_dump(
'unpack e',
unpack('e'hex2bin('0000000000000000')),
unpack('e'hex2bin('000000000000f03f')),
unpack('e'hex2bin('0080e03779c34143')),
unpack('e'hex2bin('4a6ade0d65ebe23f')),
unpack('e'hex2bin('000000000000f0bf')),
unpack('e'hex2bin('0080e03779c341c3')),
unpack('e'hex2bin('4a6ade0d65ebe2bf')),
unpack('e'hex2bin('e1639d31956ae5c3')) );
echo 
'<br><br>';

var_dump(
'unpack E',
unpack('E'hex2bin('3ff0000000000000')),
unpack('E'hex2bin('4341c37937e08000')),
unpack('E'hex2bin('3fe2eb650dde6a4a')),
unpack('E'hex2bin('43e56a95319d63e1')),
unpack('E'hex2bin('bff0000000000000')),
unpack('E'hex2bin('c341c37937e08000')),
unpack('E'hex2bin('bfe2eb650dde6a4a')),
unpack('E'hex2bin('c3e56a95319d63e1')) );
echo 
'<br><br>';

var_dump(
'unpack g',
unpack('g'hex2bin('0000803f')),
unpack('g'hex2bin('ca1b0e5a')),
unpack('g'hex2bin('285b173f')),
unpack('g'hex2bin('aa542b5f')),
unpack('g'hex2bin('000080bf')),
unpack('g'hex2bin('ca1b0eda')),
unpack('g'hex2bin('285b17bf')),
unpack('g'hex2bin('aa542bdf')) );
echo 
'<br><br>';

var_dump(
'unpack G',
unpack('G'hex2bin('3f800000')),
unpack('G'hex2bin('5a0e1bca')),
unpack('G'hex2bin('3f175b28')),
unpack('G'hex2bin('5f2b54aa')),
unpack('G'hex2bin('bf800000')),
unpack('G'hex2bin('da0e1bca')),
unpack('G'hex2bin('bf175b28')),
unpack('G'hex2bin('df2b54aa')) );

?>

  4 EXERCISE   

<?php

var_dump
(pack("Z0","f") );
echo 
'<br><br>';
var_dump(pack("Z5""foo\0") );
echo 
'<br><br>';
var_dump(pack("Z4""fooo") );
echo 
'<br><br>';
var_dump(pack("Z4""foo") );
echo 
'<br><br>';
var_dump(pack("Z*""foo") );
echo 
'<br><br>';
var_dump(unpack("Z*""foo\0\rbar\0 \t\r\n") );
echo 
'<br><br>';
var_dump(unpack("Z9""foo\0\rbar\0 \t\r\n") );
echo 
'<br><br>';
var_dump(unpack("Z2""\0") );
echo 
'<br><br>';
var_dump(unpack("Z2""\0\0") );
echo 
'<br><br>';
var_dump(unpack("Z2""A\0") );
echo 
'<br><br>';
var_dump(unpack("Z2""AB\0") );
echo 
'<br><br>';
var_dump(unpack("Z2""ABC") );

?>