1

My webhost reports that PHP_INT_MAX is 2147483647, i.e. it's a 32-bit environment. I'm trying to convert a couple of mathematical operations that currently works in a 64-bit environment, so that they also work in the 32-bit environment.

$id = '76561197996545192';
$temp = '';
for ($i = 0; $i < 8; $i++)
{
    $temp .= chr($id & 0xFF);
    $id >>= 8;
}
$result= md5('BE' . $temp);
echo $result;

The above yields de46c6d30bfa6e097fca82f63c2f4f4c in the 32-bit environment, but it should actually yield cd97cc68c1038b485b081ba2aa3ea6fa (which it currently does in the 64-bit environment). I'm guessing that the bitshift operator is causing the mismatch, but I'm not sure (I'm not a php-expert, nor a mathematician, and I'm not the author of the original code :)

BCMath is installed in the 32-bit environment, and there may be other frameworks installed as well (I can check the phpinfo if needed).

How would I go about fixing this? Is it possible?

// Linus

Edit: Yes, I know the code looks weird, but it is working exactly as intended in a 64-bit environment.

4

1 回答 1

1

在我看来,由于 $id 是一个字符串,所以按位运算并没有真正按照您的期望做。我知道它不能是整数,因为它对于 32 位系统来说太大了。也许你想要做的是处理 $id 的最后 3 个字符并将它们变成一个整数?这将是代码:

$id = '76561197996545192';
$temp = '';
for ($i = 0; $i < 8; $i++) {
    $tnbr = intval(substr($id, -3));
    $char = chr($tnbr & 0xFF); // Is the bitwise to make them valid chars? Maybe skip that part?
    $temp .= $char;
    $id = substr($id, 0, strlen($id) - 3);
}

$result = md5('BE' . $temp);
echo $result;

这给了我 98b0f4cc942bfe4a22dd7877ae3e9f06 的结果。

我不确定这个数学算法的目的是什么,但也许我不需要:)

祝你好运!/威尔

于 2014-11-03T14:21:37.083 回答