dth8312 2011-02-10 11:09
浏览 115
已采纳

PHP中的Oracle11g大整数到十六进制字符串转换

I have a column in table declared as macaddr NUMBER(15) (macaddress integer representation range - up to 15 digits)

The PHP fetch this values as a strings, obviously on 32bit system the dechex will not handle that big ints.

How I can easily convert that strings into hex macaddr representation (human redable)? And same question in opposite direction?

  • 写回答

2条回答 默认 最新

  • dongzi3805 2011-02-11 20:55
    关注

    Do you really need to store these informations in a numeric form? Modern computers have plenty of space available.

    Anyway you can use PHP to convert numbers larger than that.
    My humble solution is to avoid relying on types provided by PHP and use the BCMath Arbitrary Precision Mathematics.
    According to the manual the library comes bundled with PHP since release 4.0.4. If you run PHP in Windows it works straight. Otherwise you have to configure with --enable-bcmath .
    Thanks to user contributed notes on the PHP.net website I found these two handy functions:

    <?php
    
            /* hexadecimal to/from decimal functions from user contributed notes at http://it.php.net/manual/en/ref.bc.php */
    
            function bchexdec($hex) {
                if(strlen($hex) == 1) {
                    return hexdec($hex);
                } else {
                    $remain = substr($hex, 0, -1);
                    $last = substr($hex, -1);
                    return bcadd(bcmul(16, bchexdec($remain)), hexdec($last));
                }
            }
    
            function bcdechex($dec) {
                $last = bcmod($dec, 16);
                $remain = bcdiv(bcsub($dec, $last), 16);
    
                if($remain == 0) {
                    return dechex($last);
                } else {
                    return bcdechex($remain).dechex($last);
                }
            }
    
        echo bcdechex (130646634308);
    
        echo '<br />';
    
        echo bchexdec ('001e6b256f44');
    
    
        ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗