为什么我在 php 中尝试将一个巨大的整数转换为十六进制时得到输出 0?
Why I am getting output 0 while trying to convert a huge integer to hex in php?
我正在尝试在 php
中将 big int
转换为 hex
我试过 function
来自 How to convert a huge integer to hex in php?
<?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;
}
$int = 115792089237316195423570985008687907852837564279074904382605163141518161494336 ;
$int_to_hex = strtoupper( bcdechex ( $int )) ;
echo $int_to_hex ;
输出为0
我已经在 WAMP
和 LAMP
中尝试了上面的代码
我安装了最新的 php
、bcmath
、gmp
。
我做错了什么?
我正在尝试生成十六进制以用于创建 bitcoin address
通常是 int
115792089237316195423570985008687907852837564279074904382605163141518161494336
给出十六进制
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364140
更新 1 :
我已验证 bcmath 已安装并加载。
php -m | grep bcmath
bcmath
更新 2:
我试过了
$int = 115792089237316195423570985008687907852837564279074904382605163141518161494336 ;
echo dechex($int);
给予
0
我尝试了更小的 int
$int = 556 ;
echo dechex($int);
给予
22c
更新 3 :
正如 Mikethetechy
所建议的那样
$int = 123456789 ;
echo dechex($int);
75bcd15
$int = "123456789" ;
echo dechex($int);
75bcd15
更新 4 :
通过将 big int
放入 quotes
解决了问题
即使用
$int = '115792089237316195423570985008687907852837564279074904382605163141518161494336';
而不是
$int = 115792089237316195423570985008687907852837564279074904382605163141518161494336;
这有效。
<?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;
}
$int = '115792089237316195423570985008687907852837564279074904382605163141518161494336';
$int_to_hex = strtoupper( bcdechex ( $int )) ;
echo $int_to_hex ;
可以google达到任意精度。根据硬件和环境设置,您的系统将对浮点数和整数值有限制。我已经将 gmp 用于这样的事情 - 想法是您使用资源,字符串任何内容并以这种方式表示它以使用它。 bc 函数也需要字符串!该函数分割字符串,您对其进行操作,然后将结果连接起来形成输出。
值得一看的可能是:https://github.com/phpseclib/phpseclib/blob/master/phpseclib/Math/BigInteger.php
我正在尝试在 php
big int
转换为 hex
我试过 function
来自 How to convert a huge integer to hex in php?
<?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;
}
$int = 115792089237316195423570985008687907852837564279074904382605163141518161494336 ;
$int_to_hex = strtoupper( bcdechex ( $int )) ;
echo $int_to_hex ;
输出为0
我已经在 WAMP
和 LAMP
中尝试了上面的代码
我安装了最新的 php
、bcmath
、gmp
。
我做错了什么?
我正在尝试生成十六进制以用于创建 bitcoin address
通常是 int
115792089237316195423570985008687907852837564279074904382605163141518161494336
给出十六进制
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364140
更新 1 :
我已验证 bcmath 已安装并加载。
php -m | grep bcmath
bcmath
更新 2:
我试过了
$int = 115792089237316195423570985008687907852837564279074904382605163141518161494336 ;
echo dechex($int);
给予
0
我尝试了更小的 int
$int = 556 ;
echo dechex($int);
给予
22c
更新 3 : 正如 Mikethetechy
所建议的那样$int = 123456789 ;
echo dechex($int);
75bcd15
$int = "123456789" ;
echo dechex($int);
75bcd15
更新 4 :
通过将 big int
放入 quotes
即使用
$int = '115792089237316195423570985008687907852837564279074904382605163141518161494336';
而不是
$int = 115792089237316195423570985008687907852837564279074904382605163141518161494336;
这有效。
<?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;
}
$int = '115792089237316195423570985008687907852837564279074904382605163141518161494336';
$int_to_hex = strtoupper( bcdechex ( $int )) ;
echo $int_to_hex ;
可以google达到任意精度。根据硬件和环境设置,您的系统将对浮点数和整数值有限制。我已经将 gmp 用于这样的事情 - 想法是您使用资源,字符串任何内容并以这种方式表示它以使用它。 bc 函数也需要字符串!该函数分割字符串,您对其进行操作,然后将结果连接起来形成输出。
值得一看的可能是:https://github.com/phpseclib/phpseclib/blob/master/phpseclib/Math/BigInteger.php