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"; } for ($i=1; $i<1<<32; $i=$i<<1) { $val = $i; $enc = pack_encode($val); $dec = pack_decode($enc); echo "$enc $dec $val\n"; }
based on VUccF
Output for 4.3.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 AQ 1 1 Ag 2 2 BA 4 4 CA 8 8 EA 16 16 IA 32 32 QA 64 64 gA 128 128 AAE 256 256 AAI 512 512 AAQ 1024 1024 AAg 2048 2048 ABA 4096 4096 ACA 8192 8192 AEA 16384 16384 AIA 32768 32768 AAAB 65536 65536 AAAC 131072 131072 AAAE 262144 262144 AAAI 524288 524288 AAAQ 1048576 1048576 AAAg 2097152 2097152 AABA 4194304 4194304 AACA 8388608 8388608 AAAAAQ 16777216 16777216 AAAAAg 33554432 33554432 AAAABA 67108864 67108864 AAAACA 134217728 134217728 AAAAEA 268435456 268435456 AAAAIA 536870912 536870912 AAAAQA 1073741824 1073741824 AAAAgA 2147483648 2147483648