I'm trying to convert big int
to hex
in php
I have tried this function
from 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 ;
It gives output as 0
I've tried above code in WAMP
and LAMP
I've latest php
, bcmath
, gmp
installed.
What am I doing wrong ?
I'm trying to generate hex to use creating bitcoin address
usually int
115792089237316195423570985008687907852837564279074904382605163141518161494336
gives HEX
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364140
update 1 :
i have verified that bcmath is installed and loaded.
php -m | grep bcmath
bcmath
update 2:
i tried
$int = 115792089237316195423570985008687907852837564279074904382605163141518161494336 ;
echo dechex($int);
gives
0
i tried smaller int
$int = 556 ;
echo dechex($int);
gives
22c
update 3 : as suggested by Mikethetechy
$int = 123456789 ;
echo dechex($int);
75bcd15
$int = "123456789" ;
echo dechex($int);
75bcd15
update 4 :
Issue solved by putting big int
in quotes
i.e. using
$int = '115792089237316195423570985008687907852837564279074904382605163141518161494336';
instead of
$int = 115792089237316195423570985008687907852837564279074904382605163141518161494336;