PHP convert decimal to hexadecimal

I am extracting a series from a digital certificate using the built-in OpenSSL library, however, it is difficult for me to convert this number to hexadecimal with precision.

The extracted number is initially in decimal form, but I need to have it in hexadecimal format.

The number I'm trying to convert is: 114483222461061018757513232564608398004

Here is what I tried:

  • dechex() does not work, it returns: 7fffffffffffffff

The closest I could get was this function on the php.net page, but it does not convert its entire number.

 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 expect:

  • Decimal number: 114483222461061018757513232564608398004
  • Expected hex: 5620AAA80D50FD70496983E2A39972B4

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

I need a PHP solution.

+6
source share
4 answers

The problem is that The largest number that can be converted is ... 4294967295 - hence why it does not work for you.

This answer worked for me during a quick test, assuming you have bcmath installed on your server , and you can get the number as a string to run from . If you cannot, that is, it starts life as a numerical variable, you will immediately reach the PHP 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" 

Make sure you pass a string to this function, not a numeric one. In the example you pointed out in the question, it looks like you can get the number as a string first, so it should work if you have bc installed.

+5
source

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); 
+5
source

This is a large integer, so you need to use a library with a large integer, like GMP :

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

Try using 100% 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); ?> 
0
source

All Articles