简体   繁体   中英

Converting 64 bit integer to 32 bit on a 32 bit PHP installation?

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!

Have you tried the example code?

$and = gmp_and("0xffffffff", "76561197998705985");
echo gmp_strval($and) . "\n";

#Convert back to int
$asInt = gmp_intval($and);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM