# Conversion from decimal to hexadecimal system in PHP

There is a function to convert numbers from decimal to hexadecimal.

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

Work example:

`10-:4501598728 16-:080000010C50F2`

We need to implement this whole thing in php. What do you really think? Who will advise what? Thanks in advance.

We think further.

The sprintf function formats and stores character sets and values ​​into a variable, in our case szText. “`%02I64X%012I64X`” – format control string. Honestly, xs how to understand this.

## Answer 1, authority 100%

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

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

And 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;
}
?>
``````

## Answer 2, authority 67%

``````//
print bindec(11011); // 27
print octdec(33);    // 27
print hexdec('1b');  // 27
//
print decbin(27);    // 11011
print decoct(27);    // 33
print dechex(27);    // 1b
``````