3

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;

1 Answers1

1

This works.

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

Can google up on arbitrary precision. Your system will have limitations on floats and integer values based on hardware and environment settings. I've used gmp for things like this - ideas is you use a resource, string whatever and represent it that way to work with it. The bc functions also expect strings! That function divides up the string, you manipulate it, and then you concatenate your results to form the output.

Good thing to look at might be: https://github.com/phpseclib/phpseclib/blob/master/phpseclib/Math/BigInteger.php

ficuscr
  • 6,975
  • 2
  • 32
  • 52
  • i am not pro, to understand, why this works , but this indeed works. –  Aug 22 '17 at 04:58
  • @AMB i think it is because of this line: $int = '115792089237316195423570985008687907852837564279074904382605163141518161494336'; . you had a space at last. not sure ...but may be because of that – Manas Aug 22 '17 at 04:59
  • @Mikethetechy No the problem here is that the number simply doesn't fit into an integer: 64 bits can only store 2^64 = 18446744073709551616 different values we use half of them for negative values and the rest for positiv numbers and zero. So we use them for the integers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. The given number is still so big that we can't save it exactly so we store it as a float which saves something like 1,0 * 2^256. We can't transform that to hex anymore because it lags the digits. They aren't saved at all. If we use a string we can save all digits. – Christoph Diegelmann Aug 22 '17 at 06:02
  • @Christoph thanks a lot for the explanation! i was pretty curious as in why this wouldn't work! so string did the trick here in this case. – Manas Aug 22 '17 at 06:10
  • @Mikethetechy It's a very interesting subject. Things like precision, floating points, endianess. Good things in our field to have some grasp of. I could honestly use a refresher. – ficuscr Aug 22 '17 at 14:59
  • 1
    @ficuscr yes u r right...i started looking into it haha. good that i tried to solve this question..and now I can learn smthng from generous ppl like you :D thxx mate – Manas Aug 23 '17 at 03:55