note 109328 modified in function.pack by danbrown

From: 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(&#39;%b&#39;, 5000000000) = 100101010000001011111001000000000&#13;&#10;pack will not treat anything passed to it as 64-bit. If you want to pack a 64-bit integer:&#13;&#10;&#13;&#10;&#60;?php&#13;&#10;$big = 5000000000;&#13;&#10;&#13;&#10;$left = 0xffffffff00000000;&#13;&#10;$right = 0x00000000ffffffff;&#13;&#10;&#13;&#10;$l = ($big &#38; $left) &#62;&#62;32;&#13;&#10;$r = $big &#38; $right;&#13;&#10;&#13;&#10;$good = pack(&#39;NN&#39;, $l, $r);&#13;&#10;&#13;&#10;$urlsafe = str_replace(array(&#39;+&#39;,&#39;/&#39;), array(&#39;-&#39;,&#39;_&#39;), base64_encode($good));&#13;&#10;&#13;&#10;//done!&#13;&#10;&#13;&#10;//rebuild:&#13;&#10;$unurl = str_replace(array(&#39;-&#39;,&#39;_&#39;), array(&#39;+&#39;,&#39;/&#39;), $urlsafe);&#13;&#10;$binary = base64_decode($unurl);&#13;&#10;&#13;&#10;$set = unpack(&#39;N2&#39;, $tmp);&#13;&#10;print_r($set);&#13;&#10;&#13;&#10;$original = $set[1] &#60;&#60; 32 | $set[2];&#13;&#10;echo $original, &#34;\\r\\n&#34;;&#13;&#10;?&#62;&#13;&#10;&#13;&#10;results in:&#13;&#10;Array&#13;&#10;(&#13;&#10; [1] =&#62; 1&#13;&#10; [2] =&#62; 705032704&#13;&#10;)&#13;&#10;5000000000&#13;&#10;&#13;&#10;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

« previous php.notes (#189221) next »