Why I am getting output 0 while trying to convert a huge integer to hex in php? -




i'm trying convert big int hex in php

i have tried function how convert huge integer hex in php?

<?php  function bcdechex($dec) {     $hex = '';     {             $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 0

i've tried above code in wamp , lamp i've latest php, bcmath, gmp installed.

what doing wrong ?

i'm trying generate hex use creating bitcoin address

usually int

115792089237316195423570985008687907852837564279074904382605163141518161494336

gives hex

fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140 

update 1 :

i have verified bcmath installed , 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 : suggested mikethetechy

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

75bcd15

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

75bcd15


update 4 :

issue solved putting big int in quotes

i.e. using

$int = '115792089237316195423570985008687907852837564279074904382605163141518161494336'; 

instead of

$int = 115792089237316195423570985008687907852837564279074904382605163141518161494336; 

this works.

<?php  function bcdechex($dec) {     $hex = '';     {             $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 ; 

can google on arbitrary precision. system have limitations on floats , integer values based on hardware , environment settings. i've used gmp things - ideas use resource, string whatever , represent way work it. bc functions expect strings! function divides string, manipulate it, , concatenate results form output.

good thing @ might be: https://github.com/phpseclib/phpseclib/blob/master/phpseclib/math/biginteger.php





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -