Search code examples
phpintegerbit-manipulationbackwards-compatibility

pack format "Q" on PHP 5.5 / 32 bit machines


I would like to implement something as described here: https://webapps.stackexchange.com/a/101153 in PHP. So far the following seems to be working:

encode

return rtrim(strtr(base64_encode(pack('Q', $int64)), '+/', '-_'), '=');

decode

$bytes = base64_decode(strtr($data, '-_', '+/'));
return unpack('Q', $bytes)[1];

Unfortunately this only works on 64bit systems and PHP5.6.3+ where the "Q" format of pack/unpack was introduced.

What possibilities do I have to implement the same function that would work the same on php5.5 64 & 32 bit?


Solution

  • As pointed out by @Narf, using this: https://stackoverflow.com/a/41060369/468027 and replacing base_convert with either: http://php.net/manual/en/function.base-convert.php#106546 or http://php.net/manual/en/function.base-convert.php#109660 solved the issue for me.