What is the dechex method in PHP?

The dechex method can be used to convert a decimal number to a hexadecimal string.

Hexadecimal is the base-16 number system and uses the digits from 0 to 9 and A to F.

Syntax

dechex(int $num): string

Arguments

The argument is the decimal number to be converted to hexadecimal.

The dechex method only handles the unsigned integers. If we pass negative integers that will be treated as unsigned integers.

The largest decimal value that can be converted to decimal in a 32-bit platform is PHP_INT_MAX * 2 + 1 (or -1).

Return value

It returns the hexadecimal value of the argument as a string.

Code

<?php
$num = 15;
echo "hexadeciaml value of ". $num. " is: ". dechex($num)."\n";
$num = 16;
echo "hexadeciaml value of ". $num. " is: ". dechex($num)."\n";
$num = 100;
echo "hexadeciaml value of ". $num. " is: ". dechex($num)."\n";
$num = 999;
echo "hexadeciaml value of ". $num. " is: ". dechex($num)."\n";
?>

Explanation

In the code above, we have used the dechex method to convert the decimal numbers 15, 16, 100, and 999 into hexadecimal representations.

Free Resources