Converting from decimal to hexadecimal in PHP

There is a function for converting numbers from decimal to hexadecimal.

void ConvertNumber(__int64 nNum, char* szText)
{
    sprintf(szText,"%02I64X%012I64X",nNum&(__int64)0x00000000000000FF,(nNum>> (__int64)8));
}

Example of operation:

10-ная:4501598728 на выходе 16-ная:080000010C50F2

You need to implement all this in php. What do you think is real? Who will advise what? Thank you in advance.

We think further.

The sprintf function formats and stores sets of characters and values in a variable, in our case szText. "%02I64X%012I64X " - format control string. Honestly, xs how to understand it.

 0
php
Author: Nicolas Chabanovsky, 2011-09-04

2 answers

// преобразование в десятичную систему
 print bindec(11011); // 27
 print octdec(33);    // 27
 print hexdec('1b');  // 27
 // преобразование из десятичной системы
 print decbin(27);    // 11011
 print decoct(27);    // 33
 print dechex(27);    // 1b

Read more here

 2
Author: DL_, 2011-09-04 19:18:20

Do you need to solve this problem yourself, or will you use a ready-made function?

If ready, then here: string dechex ( int $number ), taken here.

And more from the same place, only from the comments:

//I was confused by dechex's size limitation. Here is my solution to the problem. It
//supports much bigger values, as well as signs.

<?php 
function dec_to_hex($dec) 
 { 
     $sign = ""; // suppress errors 
     if( $dec < 0){ $sign = "-"; $dec = abs($dec); }

     $hex = Array( 0 => 0, 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 
                   6 => 6, 7 => 7, 8 => 8, 9 => 9, 10 => 'a', 
                   11 => 'b', 12 => 'c', 13 => 'd', 14 => 'e',    
                   15 => 'f' );

     do 
     { 
         $h = $hex[($dec%16)] . $h; 
         $dec /= 16; 
     } 
     while( $dec >= 1 );

     return $sign . $h; 
 } 
?>
 3
Author: Dex, 2011-09-04 19:22:24