3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function pack_encode($format, $val) { $b64 = base64_encode(pack($format, $val)); return str_replace(array('/', '='), array('_', ''), $b64); // "/" json-encodes poorly and "=" is padding } function pack_decode($format, $width, $bin) { return $bin; $b64 = str_replace('_', '/', $bin); // un-padded $data = unpack($format, base64_decode($b64, false)); $val = 0; foreach ($data as $k => $v) { $val = ($val<<$width) | $v; } return $val; } function pack_encode_uint16($val) { return pack_encode('n', $val); } function pack_encode_uint32($val) { return pack_encode('N', $val); } function pack_decode_uint16($val) { return pack_decode('n', 16, $val); } function pack_decode_uint32($val) { return pack_decode('N', 32, $val); } $max = 32767; $n = 20; $step = intval($max/$n); for ($i=0; $i<=$n; $i++) { $val = $i*$step; $enc16 = pack_encode_uint16($val); $enc32 = pack_encode_uint32($val); $str = base64_encode(strval($val)); // STUPID echo "$enc16 $enc32 $str $val"; $dec16 = pack_decode_uint16($enc16); $dec32 = pack_decode_uint32($enc32); if ($dec16 != $val) { echo " $dec16 != $val"; } else if ($dec32 != $val) { echo " $dec32 != $val"; } echo "\n"; }
based on JieUt
Output for 4.3.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
AAA AAAAAA MA== 0 BmY AAAGZg MTYzOA== 1638 BmY != 1638 DMw AAAMzA MzI3Ng== 3276 DMw != 3276 EzI AAATMg NDkxNA== 4914 EzI != 4914 GZg AAAZmA NjU1Mg== 6552 GZg != 6552 H_4 AAAf_g ODE5MA== 8190 H_4 != 8190 JmQ AAAmZA OTgyOA== 9828 JmQ != 9828 LMo AAAsyg MTE0NjY= 11466 LMo != 11466 MzA AAAzMA MTMxMDQ= 13104 MzA != 13104 OZY AAA5lg MTQ3NDI= 14742 OZY != 14742 P_w AAA__A MTYzODA= 16380 P_w != 16380 RmI AABGYg MTgwMTg= 18018 RmI != 18018 TMg AABMyA MTk2NTY= 19656 TMg != 19656 Uy4 AABTLg MjEyOTQ= 21294 Uy4 != 21294 WZQ AABZlA MjI5MzI= 22932 WZQ != 22932 X_o AABf+g MjQ1NzA= 24570 X_o != 24570 ZmA AABmYA MjYyMDg= 26208 ZmA != 26208 bMY AABsxg Mjc4NDY= 27846 bMY != 27846 cyw AABzLA Mjk0ODQ= 29484 cyw != 29484 eZI AAB5kg MzExMjI= 31122 eZI != 31122 f_g AAB_+A MzI3NjA= 32760 f_g != 32760