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 HshEa
Output for 4.3.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
1 => AQ => 1 2 => Ag => 2 4 => BA => 4 8 => CA => 8 16 => EA => 16 32 => IA => 32 64 => QA => 64 128 => gA => 128 256 => AAE => 256 512 => AAI => 512 1024 => AAQ => 1024 2048 => AAg => 2048 4096 => ABA => 4096 8192 => ACA => 8192 16384 => AEA => 16384 32768 => AIA => 32768 65536 => AAAB => 65536 131072 => AAAC => 131072 262144 => AAAE => 262144 524288 => AAAI => 524288 1048576 => AAAQ => 1048576 2097152 => AAAg => 2097152 4194304 => AABA => 4194304 8388608 => AACA => 8388608 16777216 => AAAAAQ => 16777216 33554432 => AAAAAg => 33554432 67108864 => AAAABA => 67108864 134217728 => AAAACA => 134217728 268435456 => AAAAEA => 268435456 536870912 => AAAAIA => 536870912 1073741824 => AAAAQA => 1073741824 2147483648 => AAAAgA => 2147483648