Reputation: 1045
I am making a website which requires use of the Steam API. However I am having problems with converting a 64 bit integer to 32 bit in PHP.
I saw another post, with an answer here, that lead me to using this code:
$y = 76561197998705985;
$x = $y & 0xffffffff;
echo $x;
However, because I am using xampp which only has 32 bit installation, I am getting a result that is incorrect by 1 (38440256 instead of 38440257).
The other post that I linked to above mentions using the PHP GMP extension to convert on a 32 bit installation, but through the official documentation here, I see no helpful guidance for someone quite new to this sort of thing.
Some clarification/help would be greatly appreciated!
Upvotes: 1
Views: 1692
Reputation: 5291
Have you tried the example code?
$and = gmp_and("0xffffffff", "76561197998705985");
echo gmp_strval($and) . "\n";
#Convert back to int
$asInt = gmp_intval($and);
Upvotes: 1
Reputation: 32701
I suspect this being a floating point rounding issue, once a number exceeds PHP_INT_MAX
it will silently be converted to floating point and therefore lose precision, see this example on 64-bit PHP:
php> PHP_INT_MAX
int(9223372036854775807)
php> 9223372036854775807
int(9223372036854775807)
php> 9223372036854775807 + 1
double(9.2233720368548E+18)
You will have to store your number as a string to avoid this issue.
Upvotes: 0