3v4l.org

run code in 300+ PHP versions simultaneously
<?php function pack_encode($format, $val) { $b64 = base64_encode(pack($format, $val)); return str_replace(array('/', '='), array('_', ''), $b64); } function pack_decode($format, $val) { $b64 = str_replace('_', '/', $val); // un-padded return unpack($format, base64_decode($b64, false)); } function pack_encode_uint16($val) { return pack_encode('S', $val); } function pack_encode_uint32($val) { return pack_encode('N', $val); } function pack_decode_uint16($val) { return pack_decode('S', $val); } function pack_decode_uint32($val) { return pack_decode('N', $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"; }

preferences:
50.93 ms | 402 KiB | 5 Q