First of all it's important to note that PHPs dechex
function requires integer input.
On a 64bit system PHP does not have support for 64bit integers.
For example the typical PHP_INT_MAX
on a 64 bit system is:
9223372036854775807
This is also the maximum integer (!) number dechex
can accept.
If you pass in pow(2, 64) - 1
it's a float (1.844674407371E+19
) which converted to integer is integer 0
hence dechex
returns the string "0"
.
Same applies if you pass in 2^64 as string "18446744073709551615"
. It is converted to integer and it's integer value is 9223372036854775807
on 64 bit system. So dechex returns the string "7fffffffffffffff"
.
Therefore if you need to deal with these numbers, you can not use PHP's integer type and therefore dechex
is not the right tool for the job. Instead you can use bcmath and the gmp functions to achieve what you're looking for:
echo gmp_strval(bcadd(bcmul(2, PHP_INT_MAX), 1), 16), "\n";
echo gmp_strval(bcsub(bcpow(2, 64), 1), 16), "\n";