如何转换一个巨大的整数在PHP诅咒?(How to convert a huge integer t

2019-07-18 18:39发布

我如何转换这一点:

9312660682897061594767289296453011313180604726492752614962349778735928598708212386406555876878916185094145420038141655929349984786756296776268556142401047 

在基地16?

我发现这个递归函数:

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);
            }
        }

但达到100嵌套层次,即使我有xdebug.max_nesting_level = 1000在我的php.ini配置文件。

我Xdebug的配置是这样的:

;Xdebug
zend_extension = "${path}\php\php546x121216181946\php_xdebug-2.2.1-5.4-vc9.dll"
xdebug.default_enable=1
xdebug.remote_enable=1
xdebug.remote_host=127.0.0.1
xdebug.remote_port=9000
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_autostart = false
xdebug.dump_globals=1
xdebug.dump=COOKIE,FILES,GET,POST,REQUEST,SERVER,SESSION
xdebug.dump.SERVER=REMOTE_ADDR,REQUEST_METHOD,REQUEST_URI
xdebug.show_local_vars=1
xdebug.show_mem_delta=1
xdebug.collect_includes=1
xdebug.collect_vars=1
xdebug.collect_params=4
xdebug.collect_return=1
xdebug.auto_trace=0
xdebug.trace_options=0
xdebug.trace_format=0
xdebug.trace_output_dir="${path}\xdebug\trace"
xdebug.trace_output_name="trace.%t"
xdebug.profiler_enable=0
xdebug.profiler_append=1
xdebug.profiler_enable_trigger=1
xdebug.profiler_output_dir="${path}\xdebug\profiler"
xdebug.profiler_output_name="cachegrind.out.%s.%t"
xdebug.max_nesting_level = 1000
;/Xdebug

是否有人有不同的功能比这或如何,我可以转换这个整数任何暗示?

Thnak你!

Answer 1:

这是很容易修改你发现迭代而不是递归函数:

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

你的榜样$bignum转换为十六进制是: b1cf5653e79bef001acfb0f99d1f34487d16a8253e3a9971e98d46382114e8ac81b5102ab3c56be1f77d0eb754f566c0dacb23d64755e823f35411f9e14c5617



Answer 2:

第一个答案是优秀的(这使我们能够比较的结果),但我不能安装bcmath时。 所以,我发现这个功能:

function my_base_convert($numstring, $frombase, $tobase) 
{
  $chars = "0123456789abcdefghijklmnopqrstuvwxyz";
  $tostring = substr($chars, 0, $tobase);

  $length = strlen($numstring);
  $result = '';
  for ($i = 0; $i < $length; $i++)
  {
      $number[$i] = strpos($chars, $numstring{$i});
  }
  do
  {
    $divide = 0;
    $newlen = 0;
    for ($i = 0; $i < $length; $i++)
    {
        $divide = $divide * $frombase + $number[$i];
        if ($divide >= $tobase)
        {
            $number[$newlen++] = (int)($divide / $tobase);
            $divide = $divide % $tobase;
        } elseif ($newlen > 0)
        {
            $number[$newlen++] = 0;
        }
    }
    $length = $newlen;
    $result = $tostring{$divide} . $result;
  } while ($newlen != 0);
  return $result;
}

echo my_base_convert($bignum, 10, 16);

转换为十六进制的例子$ BIGNUM是一样的第一个答案:b1cf5653e79bef001acfb0f99d1f34487d16a8253e3a9971e98d46382114e8ac81b5102ab3c56be1f77d0eb754f566c0dacb23d64755e823f35411f9e14c5617

功能在这里找到https://magp.ie/2015/09/30/convert-large-integer-to-hexadecimal-without-php-math-extension/ (但有例如在一个小错误:“16,10”而不是 “10,16”)



文章来源: How to convert a huge integer to hex in php?
标签: php hex decimal