V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
MaiCong
V2EX  ›  问与答

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

  •  
  •   MaiCong · 2015-06-13 18:37:24 +08:00 · 1992 次点击
    这是一个创建于 3256 天前的主题,其中的信息可能已经有所发展或是发生改变。
    <?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位为例!

    :(

    2 条回复
    giuem
        1
    giuem  
       2015-06-13 18:39:33 +08:00 via Android   ❤️ 1
    32位最大整数是2147483647
    MaiCong
        2
    MaiCong  
    OP
       2015-06-13 18:49:13 +08:00 via iPhone
    @giuem 唉,我怎么这么笨!!问题解决了!!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1400 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 23:52 · PVG 07:52 · LAX 16:52 · JFK 19:52
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.