note 109328 modified in function.pack by danbrown
| From: | danbrown@php.net | Date: | Mon, 16 Jul 2012 15:45:27 +0000 |
| Subject: | note 109328 modified in function.pack by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-189221@lists.php.net to get a copy of this message | ||
Even though in a 64-bit architecure intval(6123456789) = 6123456789, and
sprintf('%b', 5000000000) = 100101010000001011111001000000000 pack
will not treat anything passed to it as 64-bit. If you want to pack a 64-bit
integer: <?php $big =
5000000000; $left = 0xffffffff00000000; $right =
0x00000000ffffffff; $l = ($big & $left)
>>32; $r = $big &
$right; $good = pack('NN', $l,
$r); $urlsafe =
str_replace(array('+','/'),
array('-','_'),
base64_encode($good)); //done! //rebuild: $unurl
= str_replace(array('-','_'),
array('+','/'), $urlsafe); $binary =
base64_decode($unurl); $set = unpack('N2',
$tmp); print_r($set); $original = $set[1]
<< 32 | $set[2]; echo $original,
"\\r\\n"; ?> results
in: Array ( [1] =>
1 [2] =>
705032704 ) 5000000000 but ONLY
on a 64-bit enabled machine and PHP distro.
--was--
Even though in a 64-bit architecure intval(6123456789) = 6123456789, and sprintf('%b',
5000000000) = 100101010000001011111001000000000
pack will not treat anything passed to it as 64-bit. If you want to pack a 64-bit integer:
$big = 5000000000;
$left = 0xffffffff00000000;
$right = 0x00000000ffffffff;
$l = ($big & $left) >>32;
$r = $big & $right;
$good = pack('NN', $l, $r);
$urlsafe = str_replace(array('+','/'), array('-','_'),
base64_encode($good));
//done!
//rebuild:
$unurl = str_replace(array('-','_'), array('+','/'),
$urlsafe);
$binary = base64_decode($unurl);
$set = unpack('N2', $tmp);
print_r($set);
$original = $set[1] << 32 | $set[2];
echo $original, "\r\n";
results in:
Array
(
[1] => 1
[2] => 705032704
)
5000000000
but ONLY on a 64-bit enabled machine and PHP distro.
http://php.net/manual/en/function.pack.php