PHP dechex() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

The dechex() function turns a normal decimal number into a hexadecimal (base 16) string.

Hexadecimal uses the digits 0-9 and the letters a-f, and it is often used for colors, memory addresses and short IDs.

Syntax

Call dechex() with the values you need. Start with this line.

PHP
You pass one whole number to dechex() and it gives you back a string of hex digits.
CallWhat it does
dechex(255)Returns "ff", the hex form of 255.
dechex(10)Returns "a", because 10 is the first letter digit in hex.
dechex(0)Returns "0".

The result is always lowercase. Use strtoupper() if you want capital letters like FF.

Library declarations
PHP
dechex(int $num): string

Parameters

ParameterDescription
numRequired. The decimal integer you want to convert to hexadecimal.

Return Value

Return ValueDescription
stringThe hexadecimal representation of num, written in lowercase without any 0x prefix.

PHP Examples

Example 1 — Convert decimal numbers to hex

Start with a few simple numbers and see what hex strings come back.

PHP
<?php
echo dechex(255) . PHP_EOL;
echo dechex(10) . PHP_EOL;
echo dechex(4095) . PHP_EOL;

How It Works

  1. dechex(255) converts 255 into base 16, which is "ff".
  2. dechex(10) gives "a" because a single hex digit can go up to f (15).
  3. dechex(4095) gives "fff" since 4095 is 16 x 16 x 16 minus 1.
  4. PHP_EOL adds a new line after each result so the output is easy to read.

Example 2 — Zero, negative and very large numbers

See how dechex() behaves at the edges of the integer range on a 64-bit system.

PHP
<?php
echo dechex(0) . PHP_EOL;
echo dechex(-1) . PHP_EOL;
echo dechex(PHP_INT_MAX) . PHP_EOL;

How It Works

  1. dechex(0) returns the string "0", not an empty string.
  2. dechex(-1) treats -1 as an unsigned number, so every bit is 1 and you get sixteen f characters.
  3. dechex(PHP_INT_MAX) returns the biggest signed 64-bit value in hex.
  4. On a 32-bit build the strings would be shorter because integers are smaller.

Example 3 — Build a hex color code

Turn red, green and blue values into a CSS color such as #FF8000.

PHP
<?php
$red = 255;
$green = 128;
$blue = 0;

$hex = "#"
  . str_pad(dechex($red), 2, "0", STR_PAD_LEFT)
  . str_pad(dechex($green), 2, "0", STR_PAD_LEFT)
  . str_pad(dechex($blue), 2, "0", STR_PAD_LEFT);

echo strtoupper($hex) . PHP_EOL;

How It Works

  1. Each color channel is a number from 0 to 255.
  2. dechex() converts each channel to hex, but 0 becomes "0" which is only one character.
  3. str_pad() adds a leading zero so every channel is exactly two characters long.
  4. strtoupper() makes the final color code uppercase, giving #FF8000.

Frequently Asked Questions

It converts a decimal integer into a hexadecimal string. For example dechex(255) returns "ff".
No. It only returns the digits. If you want a prefix, add it yourself, such as "0x" . dechex(255).
Wrap the result in strtoupper(), like strtoupper(dechex(255)), which returns "FF".
Use hexdec(). For example hexdec("ff") returns 255.
It accepts them, but they are treated as unsigned values. On a 64-bit system dechex(-1) gives "ffffffffffffffff".

Next: decoct()

The decoct() function converts a decimal number into an octal (base 8) string.

decoct() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful