user3436467
user3436467

Reputation: 1775

php convert decimal to hexadecimal

I am extracting a serial from a digital certificate using the built-in OpenSSL library, however, I am having trouble converting this number to hex with precision.

The extracted number is originally in decimal but I need to have it in hex.

The number I am trying to convert is: 114483222461061018757513232564608398004

Here is what I've tried:

The closest I could get was this function from the php.net page but it does not convert the whole number on part of it.

function dec2hex($dec) {
  $hex = ($dec == 0 ? '0' : '');

  while ($dec > 0) {
    $hex = dechex($dec - floor($dec / 16) * 16) . $hex;
    $dec = floor($dec / 16);
  }

  return $hex;
}
echo dec2hex('114483222461061018757513232564608398004');
//Result: 5620aaa80d50fc000000000000000000

Here is what I am expecting:

I can see the correction conversion here: https://www.mathsisfun.com/binary-decimal-hexadecimal-converter.html

I need a PHP solution.

Upvotes: 7

Views: 9540

Answers (4)

Ganymede
Ganymede

Reputation: 165

Answered by lafor. How to convert a huge integer to hex in 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;
}

Example:
$decimal = '114483222461061018757513232564608398004';
echo "Hex decimal : ".bcdechex($decimal);

Upvotes: 5

Jinku
Jinku

Reputation: 11

Try this 100% working for any number

 <?php 
        $dec = '114483222461061018757513232564608398004';
     // init hex array
       $hex = array();
       while ($dec)
       {
         // get modulus // based on docs both params are string
          $modulus = bcmod($dec, '16');
          // convert to hex and prepend to array
          array_unshift($hex, dechex($modulus));
         // update decimal number
         $dec = bcdiv(bcsub($dec, $modulus), 16);
        }
       // array elements to string
       echo implode('', $hex);

?>

Upvotes: 0

scrowler
scrowler

Reputation: 24435

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.

Upvotes: 8

kennytm
kennytm

Reputation: 523794

This is a big integer, so you need to use a big-integer library like GMP:

echo gmp_strval('114483222461061018757513232564608398004', 16);
// output: 5620aaa80d50fd70496983e2a39972b4

Upvotes: 5

Related Questions