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
andA to F
.
dechex(int $num): string
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)
.
It returns the hexadecimal value of the argument as a string.
<?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";?>
In the code above, we have used the dechex
method to convert the decimal numbers 15
, 16
, 100
, and 999
into hexadecimal representations.