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 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?