我来求个 PHP 32 位与 64 位 按位异或 操作的问题.....

2015-06-13 18:37:24 +08:00
 MaiCong
<?php
function t_charCodeAt($str, $index){
    $char = mb_substr($str, $index, 1, 'UTF-8');
    if (mb_check_encoding($char, 'UTF-8')) {
        $ret = mb_convert_encoding($char, 'UTF-32BE', 'UTF-8');
        return hexdec(bin2hex($ret));
    } else {
        return null;
    }
}
function t_crc32($g){
    $g = urlencode($g);
    $e = array();
    for ($f = 0; $f < 256; $f++) {
        $c = $f;
        for ($d = 0; $d < 8; $d++) {
            $c = ($c & 1) ? (($c >> 1) & 2147483647) ^ 3988292384 : (($c >> 1) & 2147483647);
        }
        $e[$f] = $c;
    }
    for ($f = 0, $c = 4294967295; $f < strlen($g); $f++) {
        $c = (($c >> 8) & 16777215) ^ $e[($c & 255) ^ t_charCodeAt($g, $f)];
    }
    $c ^= 4294967295; // 32位和64位结果不一致,求解决办法!正确结果应该以32位为例!
    $prefix = (!is_numeric(substr($c >> 3, 0, 1))) ? substr($c >> 3, 0, 1) : '';
    echo $prefix.base_convert($c >> 3, 10, 16);   
}
t_crc32('1029409')."\n";
t_crc32('1549058');

32位和64位结果不一致,求解决办法!正确结果应该以32位为例!

:(

2000 次点击
所在节点    问与答
2 条回复
giuem
2015-06-13 18:39:33 +08:00
32位最大整数是2147483647
MaiCong
2015-06-13 18:49:13 +08:00
@giuem 唉,我怎么这么笨!!问题解决了!!

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/198288

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX