3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function pack_encode($val) { $str = ''; while ($val > 0) { $str .= chr($val & 0xFF); $val = $val >> 8; } // "/" json-encodes poorly and "=" is padding return str_replace( array('/', '='), array('_', ''), base64_encode($str) ); } function pack_decode($bin) { $str = base64_decode(str_replace('_', '/', $bin)); $val = 0; for ($i=0; $i<strlen($str); $i++) { $val |= (ord($str[$i]) << ($i * 8)); } return $val; } for ($i=0; $i<32; $i++) { $val = 1<<$i; $enc = pack_encode($val); $dec = pack_decode($enc); echo "$val => $enc => $dec\n"; } /* $arr = array(); $packed = pack('N', 32767); for ($i=0; $i<strlen($packed); $i++) { $arr[$i] = ord($packed[$i]); } print_r($arr); function pack_encode($val) { $b64 = base64_encode(pack($format, $val)); return str_replace(array('/', '='), array('_', ''), $b64); // "/" json-encodes poorly and "=" is padding } function pack_decode($bin) { $b64 = str_replace('_', '/', $bin); // un-padded $data = unpack('C'.(length($bin)/2), base64_decode($b64, false)); $val = 0; foreach ($data as $v) { $val = ($val<<$width) | $v; } return $val; } $max = 32767; $n = 20; $step = intval($max/$n); for ($i=0; $i<=$n; $i++) { $val = $i*$step; $enc = pack_encode($val); $dec = pack_decode($val); echo "$enc $dec $val\n"; } */
based on Qjfed
Output for 7.0.0 - 7.1.0
Fatal error: Uncaught Error: Call to undefined function  base64_decode() in /in/HshEa:18 Stack trace: #0 /in/HshEa(29): pack_decode('AQ') #1 {main} thrown in /in/HshEa on line 18
Process exited with code 255.
Output for 5.0.0 - 5.6.28, hhvm-3.10.0 - 3.12.0
Fatal error: Call to undefined function  base64_decode() in /in/HshEa on line 18
Process exited with code 255.
Output for 4.4.5 - 4.4.9
Fatal error: Call to undefined function:  base64_decode() in /in/HshEa on line 18
Process exited with code 255.
Output for 4.3.2 - 4.4.4
Fatal error: Call to undefined function:  base64_decode() in /in/HshEa on line 18
Process exited with code 255.
Output for 4.3.0 - 4.3.1
Fatal error: Call to undefined function:  base64_decode() in /in/HshEa on line 18