简体   繁体   English

从 64 位数字转换为 32 位数字

[英]Convert from 64bit number to 32bit number

Trying a lot and just failing..尝试了很多,只是失败了..

$x = 76561198005785475;

I want to this number, turn into this:我要这个号,变成这个:

$y = 45519747;

That is the 32bit form of it.那是它的 32 位形式。


Trying to explain with more details:试图解释更多细节:

http://www.tonymarston.net/php-mysql/converter.php http://www.tonymarston.net/php-mysql/converter.php

1) Put the value 76561198005785475 on the "Decimal (input)" field. 1) 将值 76561198005785475 放在“十进制(输入)”字段中。 2) Press "DEC to BIN" on the "Binary (Base 2)" field. 2) 在“Binary (Base 2)”字段上按“DEC to BIN”。 3) Count 32 starting from the RIGHT and copy it. 3) 从右边开始数 32 并复制它。 4) Paste the 32 chars binary number on "Binary (Base 2)" field. 4) 将 32 个字符的二进制数粘贴到“Binary (Base 2)”字段上。 5) Press "Bin to Dec" button on the "Binary (Base 2)" field. 5) 在“Binary (Base 2)”字段上按“Bin to Dec”按钮。

Ok, now you can see the "45519747" number.好的,现在您可以看到“45519747”号码。

Try this:尝试这个:

$y = $x & 0xffffffff;

This will truncate your 64-bit value to a 32-bit value, but note that there is absolutely no way to get the 64-bit value back, this is a destructive method.这会将您的 64 位值截断为 32 位值,但请注意,绝对无法恢复 64 位值,这是一种破坏性方法。

I tried many solution.我尝试了很多解决方案。 But no one help me.但是没有人帮我。 Finally, following script save my life.最后,以下脚本挽救了我的生命。

function intval32bits($value)
{
    $value = ($value & 0xFFFFFFFF);

    if ($value & 0x80000000)
        $value = -((~$value & 0xFFFFFFFF) + 1);

    return $value;
}

This is too long to write into a comment, so I'll post it here instead.写到评论里太长了,所以我把它贴在这里。

@Kolink has the right answer; @Kolink 有正确的答案; what you want is that operation.你想要的是那个操作。 However, note that because your $x is too big to be stored in an int format anyway, it'll be held as a float in 32-bit computers, and float s suffer from precision loss.但是,请注意,由于您的$x太大而无法以int格式存储,因此它将在 32 位计算机中作为float保存,并且float s 会遭受精度损失。 My pet theory on why you get 45519744 instead of the right answer is that your 32-bit computer lost the precision on the last digits.我关于为什么得到 45519744 而不是正确答案的宠物理论是,您的 32 位计算机失去了最后一位数字的精度。 To see this in action, try this here :要查看此操作,请在此处尝试:

$x = 76561198005785475;
echo (int)$x;

That site uses a 32-bit server, and it returns 45519744. This demonstrates the precision loss.该站点使用 32 位服务器,返回 45519744。这说明了精度损失。

On the other hand, if you go here and run:另一方面,如果你去这里运行:

$x = 76561198005785475;
$y = $x & 0xffffff;

You get the right answer, because that site is 64-bit.您会得到正确的答案,因为该站点是 64 位的。

If you want to do the operation on a 32-bit machine (as you evidently do), I suggest you use gmp_and from the PHP GMP extension.如果您想在 32 位机器上执行操作(正如您显然所做的那样),我建议您使用 PHP GMP 扩展中的gmp_and Unfortunately I couldn't test to see if it works - I'll leave that to you.不幸的是,我无法测试它是否有效 - 我会把它留给你。

I had something similar.我有类似的事情。 I was want to consider Overflow when converting from Int64 to Int32.从 Int64 转换为 Int32 时,我想考虑溢出。 this code worked well for me:这段代码对我来说效果很好:

function intval32bits($value)
{
    $value = ($value & 0xFFFFFFFF);

    if ($value & 0x80000000)
        $value = -((~$value & 0xFFFFFFFF) + 1);

    return $value;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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