How to Convert a Huge Integer to Hex in PHP

How to convert a huge integer to hex in php?

It's pretty easy to modify the function you found to be iterative rather than recursive:

function bcdechex($dec) {
$hex = '';
do {
$last = bcmod($dec, 16);
$hex = dechex($last).$hex;
$dec = bcdiv(bcsub($dec, $last), 16);
} while($dec>0);
return $hex;
}

Your example $bignum converted to hex is:
b1cf5653e79bef001acfb0f99d1f34487d16a8253e3a9971e98d46382114e8ac81b5102ab3c56be1f77d0eb754f566c0dacb23d64755e823f35411f9e14c5617

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

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

php convert decimal to hexadecimal

The problem is that The largest number that can be converted is ... 4294967295 - hence why it's not working for you.

This answer worked for me during a quick test, assuming you have bcmath installed on your server, and you can obtain the number as a string to start with. If you can't, i.e. it begins life as numeric variable, you'll immediately reach PHP's float limit.

// Credit: joost at bingopaleis dot com
// Input: A decimal number as a String.
// Output: The equivalent hexadecimal number as a String.
function dec2hex($number)
{
$hexvalues = array('0','1','2','3','4','5','6','7',
'8','9','A','B','C','D','E','F');
$hexval = '';
while($number != '0')
{
$hexval = $hexvalues[bcmod($number,'16')].$hexval;
$number = bcdiv($number,'16',0);
}
return $hexval;
}

Example:

$number = '114483222461061018757513232564608398004'; // Important: already a string!
var_dump(dec2hex($number)); // string(32) "5620AAA80D50FD70496983E2A39972B4"

Ensure you pass a string into that function, not a numeric variable. In the example you provided in the question, it looks like you can obtain the number as a string initially, so should work if you have bc installed.

PHP convert large Decimal number to Hexadecimal

Your SerialNumber is a Math_BigInteger object as the var_dump shows.
Use the toHex method to retrieve the contained number in a hexadecimal format.
See reference on PEAR website.

$serial = $cert['tbsCertificate']['serialNumber'];
$valueInHex = $serial->toHex();

Note: 118045041395046077749311747456482878735 in decimal format equals to 58CEA5E36351B91F49E47A20CEFF250F in hexadecimal format. You may easily check that with an online converter like this.

Large hex values with PHP hexdec

As said on the hexdec manual page:

The function can now convert values
that are to big for the platforms
integer type, it will return the value
as float instead in that case.

If you want to get some kind of big integer (not float), you'll need it stored inside a string. This might be possible using BC Math functions.

For instance, if you look in the comments of the hexdec manual page, you'll find this note

If you adapt that function a bit, to avoid a notice, you'll get:

function bchexdec($hex)
{
$dec = 0;
$len = strlen($hex);
for ($i = 1; $i <= $len; $i++) {
$dec = bcadd($dec, bcmul(strval(hexdec($hex[$i - 1])), bcpow('16', strval($len - $i))));
}
return $dec;
}

(This function has been copied from the note I linked to; and only a bit adapted by me)

And using it on your number:

$h = 'D5CE3E462533364B';
$f = bchexdec($h);
var_dump($f);

The output will be:

string '15406319846273791563' (length=20)

So, not the kind of big float you had ; and seems OK with what you are expecting:

Result from calc.exe =
15406319846273791563


Hope this help ;-)

And, yes, user notes on the PHP documentation are sometimes a real gold mine ;-)

Convert Integer to hex values in PHP

I'm a little confused here... 0x01 = 1.
0x020 = 32, etc.

Are you looking for some of the string formatters like available in printf/sprintf? You can use the %x marker to output hex values.

edit: Also, yes, your conversions are correct.

Oracle11g large integer to hex string conversion in php

Do you really need to store these informations in a numeric form? Modern computers have plenty of space available.

Anyway you can use PHP to convert numbers larger than that.

My humble solution is to avoid relying on types provided by PHP and use the BCMath Arbitrary Precision Mathematics.

According to the manual the library comes bundled with PHP since release 4.0.4. If you run PHP in Windows it works straight. Otherwise you have to configure with --enable-bcmath .

Thanks to user contributed notes on the PHP.net website I found these two handy functions:

<?php

/* hexadecimal to/from decimal functions from user contributed notes at http://it.php.net/manual/en/ref.bc.php */

function bchexdec($hex) {
if(strlen($hex) == 1) {
return hexdec($hex);
} else {
$remain = substr($hex, 0, -1);
$last = substr($hex, -1);
return bcadd(bcmul(16, bchexdec($remain)), hexdec($last));
}
}

function bcdechex($dec) {
$last = bcmod($dec, 16);
$remain = bcdiv(bcsub($dec, $last), 16);

if($remain == 0) {
return dechex($last);
} else {
return bcdechex($remain).dechex($last);
}
}

echo bcdechex (130646634308);

echo '<br />';

echo bchexdec ('001e6b256f44');

?>


Related Topics



Leave a reply



Submit