【问题标题】:php convert decimal to hexadecimalphp将十进制转换为十六进制
【发布时间】:2016-08-29 04:14:04
【问题描述】:

我正在使用内置 OpenSSL 库从数字证书中提取序列,但是,我无法将此数字精确地转换为十六进制。

提取的数字最初是十进制的,但我需要十六进制。

我要转换的数字是:114483222461061018757513232564608398004

这是我尝试过的:

  • dechex() 无效,返回:7fffffffffffffff

我能得到的最接近的是 php.net 页面中的这个函数,但它不会转换部分的整数。

function dec2hex($dec) {
  $hex = ($dec == 0 ? '0' : '');

  while ($dec > 0) {
    $hex = dechex($dec - floor($dec / 16) * 16) . $hex;
    $dec = floor($dec / 16);
  }

  return $hex;
}
echo dec2hex('114483222461061018757513232564608398004');
//Result: 5620aaa80d50fc000000000000000000

这是我所期待的:

  • 十进制数:114483222461061018757513232564608398004
  • 预期的十六进制数:5620AAA80D50FD70496983E2A39972B4

我可以在这里看到校正转换: https://www.mathsisfun.com/binary-decimal-hexadecimal-converter.html

我需要一个 PHP 解决方案。

【问题讨论】:

  • 如何获取号码?你能以字符串的形式获取它(不强制转换)吗?
  • 这里是数字证书:pastebin.com/34Tgs6Lw$cert = openssl_x509_parse($certdata); 然后echo $cert['serialNumber'];

标签: php


【解决方案1】:

问题是that The largest number that can be converted is ... 4294967295 - 因此它对你不起作用。

This answer 在快速测试期间为我工作,假设您的服务器上安装了 bcmath,并且您可以获得作为开头的字符串的数字。如果你不能,即它以数字变量的形式开始,你将立即到达PHP's float limit

// Credit: joost at bingopaleis dot com
// 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;
}

例子:

$number = '114483222461061018757513232564608398004'; // Important: already a string!
var_dump(dec2hex($number)); // string(32) "5620AAA80D50FD70496983E2A39972B4"

确保将字符串传递给该函数,而不是数字变量。在您在问题中提供的示例中,看起来您最初可以将数字作为字符串获取,因此如果您安装了 bc 应该可以工作。

【讨论】:

  • 在安装 bcmatch 并重新启动 apache 后它可以工作。必须重启apache。
【解决方案2】:

由拉福回答。 How to convert a huge integer to hex in php?

function bcdechex($dec) 
{
    $hex = '';
    do {    
        $last = bcmod($dec, 16);
        $hex = dechex($last).$hex;
        $dec = bcdiv(bcsub($dec, $last), 16);
    } while($dec>0);
    return $hex;
}

Example:
$decimal = '114483222461061018757513232564608398004';
echo "Hex decimal : ".bcdechex($decimal);

【讨论】:

  • 我记得在发布之前看到过这个,但当时它对我不起作用。我刚刚安装了 bcmath 并重新启动了 apache,现在它可以工作了
【解决方案3】:

这是一个大整数,所以你需要使用像GMP这样的大整数库:

echo gmp_strval('114483222461061018757513232564608398004', 16);
// output: 5620aaa80d50fd70496983e2a39972b4

【讨论】:

  • 如果输入不是字符串,此示例是否有效?
  • 收到此错误:Fatal error: Uncaught Error: Call to undefined function gmp_strval() in /var/www/html/index.php Stack trace: #0 {main}
  • @RobbieAverill:如果不是字符串,输入是什么? PHP 的整数是 32 位或 64 位长,而 OP 的数字是 128 位。
  • 正是:) 但没关系,他可以将它作为字符串获取。
  • @user3436467:看起来像你的 PHP isn't compiled with GMP support 然后。
【解决方案4】:

试试这个 100% 为任何数字工作

 <?php 
        $dec = '114483222461061018757513232564608398004';
     // init hex array
       $hex = array();
       while ($dec)
       {
         // get modulus // based on docs both params are string
          $modulus = bcmod($dec, '16');
          // convert to hex and prepend to array
          array_unshift($hex, dechex($modulus));
         // update decimal number
         $dec = bcdiv(bcsub($dec, $modulus), 16);
        }
       // array elements to string
       echo implode('', $hex);

?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-28
    • 1970-01-01
    • 2015-06-25
    • 2013-03-10
    • 2018-09-04
    • 2017-07-31
    相关资源
    最近更新 更多