dechex() function in PHP

The dechex() function converts decimal numbers to their hexadecimal representation. It returns the hexadecimal string equivalent of the specified decimal value.

Syntax

dechex(number)

Parameters

  • number − The decimal value to be converted. Can be an integer or numeric string.

Return Value

Returns a string containing the hexadecimal representation of the given decimal number.

Example

Here's how to convert decimal numbers to hexadecimal format −

<?php
    echo dechex(15) . "<br>";
    echo dechex(1990) . "<br>";
    echo dechex(255) . "<br>";
    echo dechex(0) . "<br>";
?>
f
7c6
ff
0

Working with Different Number Types

The function accepts both integers and numeric strings −

<?php
    // Integer input
    echo "Decimal 16 = " . dechex(16) . "<br>";
    
    // String input
    echo "Decimal '32' = " . dechex("32") . "<br>";
    
    // Large number
    echo "Decimal 4095 = " . dechex(4095) . "<br>";
?>
Decimal 16 = 10
Decimal '32' = 20
Decimal 4095 = fff

Conclusion

The dechex() function provides a simple way to convert decimal numbers to hexadecimal format, returning lowercase hexadecimal strings. It's commonly used in color codes, memory addresses, and base conversion operations.

Updated on: 2026-03-15T07:26:41+05:30

163 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements