V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
eecjimmy
V2EX  ›  PHP

PHP 的 base_convert 问题, 应该如何处理?

  •  
  •   eecjimmy · 2018-09-02 15:53:41 +08:00 · 2059 次点击
    这是一个创建于 2274 天前的主题,其中的信息可能已经有所发展或是发生改变。
    $dec = '200000000000000000000000';
    $hex = '2a5a058fc295ec000000';
    echo '$dec = ' . $dec . "\n";
    echo '$hex = ' . $hex . "\n";
    echo 'strcmp(base_convert($dec,10,16), $hex) === 0 : ' . var_export(strcmp(base_convert($dec, 10, 16), $hex) === 0, true) . "\n";
    echo 'strcmp(base_convert($hex,16,10), $hex) === 0 : ' . var_export(strcmp(base_convert($hex, 16, 10), $hex) === 0, true) . "\n";
    

    执行结果如下:

    $dec = 200000000000000000000000
    $hex = 2a5a058fc295ec000000
    strcmp(base_convert($dec,10,16), $hex) === 0 : true
    strcmp(base_convert($hex,16,10), $hex) === 0 : false
    
    

    手册上面是写了这样的警告, 如果数据大的话, 会出现精度丢失.

    第 1 条附言  ·  2018-09-03 11:14:47 +08:00

    已经处理了. 代码贴出来

    // Input: A decimal number as a String.
    // Output: The equivalent hexadecimal number as a String.
    function dec2hex($number)
    {
        $hexvalues = array('0','1','2','3','4','5','6','7',
                   '8','9','A','B','C','D','E','F');
        $hexval = '';
         while($number != '0')
         {
            $hexval = $hexvalues[bcmod($number,'16')].$hexval;
            $number = bcdiv($number,'16',0);
        }
        return $hexval;
    }
    
    // Input: A hexadecimal number as a String.
    // Output: The equivalent decimal number as a String.
    function hex2dec($number)
    {
        $decvalues = array('0' => '0', '1' => '1', '2' => '2',
                   '3' => '3', '4' => '4', '5' => '5',
                   '6' => '6', '7' => '7', '8' => '8',
                   '9' => '9', 'A' => '10', 'B' => '11',
                   'C' => '12', 'D' => '13', 'E' => '14',
                   'F' => '15');
        $decval = '0';
        $number = strrev($number);
        for($i = 0; $i < strlen($number); $i++)
        {
            $decval = bcadd(bcmul(bcpow('16',$i,0),$decvalues[$number{$i}]), $decval);
        }
        return $decval;
    }
    
    5 条回复    2018-09-03 11:13:43 +08:00
    eecjimmy
        1
    eecjimmy  
    OP
       2018-09-02 16:01:32 +08:00
    但是需要把大数字转换为 16 进制, 应当如何处理呢?
    nullen
        2
    nullen  
       2018-09-02 16:04:50 +08:00   ❤️ 1
    feiyuanqiu
        3
    feiyuanqiu  
       2018-09-02 16:25:45 +08:00   ❤️ 1
    jhdxr
        4
    jhdxr  
       2018-09-02 17:16:26 +08:00   ❤️ 1
    http://php.net/manual/zh/function.base-convert.php#51077

    function gmp_convert($num, $base_a, $base_b)
    {
    return gmp_strval ( gmp_init($num, $base_a), $base_b );
    }
    eecjimmy
        5
    eecjimmy  
    OP
       2018-09-03 11:13:43 +08:00
    @nullen gmp 好像没相应的方法.
    @jhdxr @feiyuanqiu 谢谢, 已经解决了.
    我把链接里面的代码贴出来了.
    ```php
    // Input: A decimal number as a String.
    // Output: The equivalent hexadecimal number as a String.
    function dec2hex($number)
    {
    $hexvalues = array('0','1','2','3','4','5','6','7',
    '8','9','A','B','C','D','E','F');
    $hexval = '';
    while($number != '0')
    {
    $hexval = $hexvalues[bcmod($number,'16')].$hexval;
    $number = bcdiv($number,'16',0);
    }
    return $hexval;
    }

    // Input: A hexadecimal number as a String.
    // Output: The equivalent decimal number as a String.
    function hex2dec($number)
    {
    $decvalues = array('0' => '0', '1' => '1', '2' => '2',
    '3' => '3', '4' => '4', '5' => '5',
    '6' => '6', '7' => '7', '8' => '8',
    '9' => '9', 'A' => '10', 'B' => '11',
    'C' => '12', 'D' => '13', 'E' => '14',
    'F' => '15');
    $decval = '0';
    $number = strrev($number);
    for($i = 0; $i < strlen($number); $i++)
    {
    $decval = bcadd(bcmul(bcpow('16',$i,0),$decvalues[$number{$i}]), $decval);
    }
    return $decval;
    }
    ```
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1352 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 17:39 · PVG 01:39 · LAX 09:39 · JFK 12:39
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.