为什么我在php中尝试将一个大整数转换为十六进制时得到输出0?

为什么我在php中尝试将一个大整数转换为十六进制时得到输出0?,php,hex,biginteger,Php,Hex,Biginteger,我正在尝试在php中将big int转换为hex 我已经尝试了这个功能 更新1: 我已验证是否已安装并加载bcmath php -m | grep bcmath bcmath 更新2: 我试过了 $int = 115792089237316195423570985008687907852837564279074904382605163141518161494336 ; echo dechex($int); 给予 我试过更小的整数 $int = 556 ; echo dechex($int

我正在尝试在
php
中将
big int
转换为
hex

我已经尝试了这个
功能


更新1:

我已验证是否已安装并加载bcmath

php -m | grep bcmath
bcmath

更新2:

我试过了

$int = 115792089237316195423570985008687907852837564279074904382605163141518161494336 ;
echo dechex($int);
给予

我试过更小的整数

$int = 556 ;
echo dechex($int);
给予


更新3: 正如Mikethetechy所建议的那样

$int = 123456789 ;
echo dechex($int);
75bcd15

$int = "123456789" ;
echo dechex($int);
75bcd15

$int = "123456789" ;
echo dechex($int);

更新4:

big int
放入
quotes

i、 e.使用

$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 ;

在转换为upper之前它提供了什么?您是否尝试过$int_to_hex=bcdechex($int);首先?@Mikethetechy它给出0,甚至在strotupper之前。@AlivetoDie my phpinfo显示BCMath支持-启用,BCMath.scale本地值0,master值0这个十进制($int);我不赞成理解为什么这样做,但这确实有效。@AMB我认为这是因为这行:$int='11579208923731619542357098500868790787852837564279049438260516314118161494336'。你终于有空了。不确定…但可能是因为that@Mikethetechy不,这里的问题是数字根本不适合整数:64位只能存储2^64=18446744073709551616个不同的值,其中一半用于负值,其余用于正数和零。所以我们将它们用于-9223372036854775808到9223372036854775807之间的整数。给定的数字仍然太大,我们无法准确地保存它,所以我们将其存储为一个浮点值,这样可以保存类似于1,0*2^256的内容。我们不能再将其转换为十六进制,因为它滞后于数字。他们根本没有得救。如果我们使用字符串,我们可以保存所有数字。@Christoph非常感谢您的解释!我很好奇为什么这行不通!所以在这个例子中,string做了这个把戏。@ficuscr是的,你是对的……我开始研究它哈哈。很好,我试着解决了这个问题。现在我可以从像你这样慷慨的人那里学到很多东西了:D thxx伙伴
$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 ;