dechex()
Today, we will discuss the dechex() function in PHP. This function is used to convert a decimal number to a hexadecimal number.
The PHP dechex() function converts a decimal (base-10) integer into its hexadecimal (base-16) string representation. Hexadecimal is the notation behind CSS colors (#ff0000), memory addresses, byte dumps, and many file and network formats, so dechex() is the go-to helper whenever you need to show a number in base 16.
This chapter covers the function's syntax, how it handles negative numbers and floats, and the common ways it is used in real code.
Syntax
dechex(int $num): string$num— the decimal integer to convert. It is interpreted as a 64-bit integer, so the largest exactly-representable value isPHP_INT_MAX(9223372036854775807).- Return value — a lowercase hexadecimal string (digits
0-9and lettersa-f). The result has no0xprefix and no leading zeros.
Basic Example
255 is the largest value that fits in a single byte, so it converts to ff — the two highest hex digits. A few more conversions make the pattern clear:
<?php
echo dechex(0); // 0
echo "\n";
echo dechex(10); // a
echo "\n";
echo dechex(16); // 10
echo "\n";
echo dechex(255); // ff
?>Note that dechex(16) returns "10" — that is "one-zero" in base 16 (16 in decimal), not the decimal number ten.
Negative Numbers and Floats
dechex() only works with integers, but it does not reject other input — it coerces it first, which can surprise you:
- Floats are truncated toward zero before conversion.
dechex(20.5)is treated as20and returns14. - Negative numbers are interpreted as their unsigned two's-complement representation. On a 64-bit build,
dechex(-1)returnsffffffffffffffff(16fdigits), not a value with a minus sign.
<?php
echo dechex(20.5); // 14 (the 20.5 is truncated to 20)
echo "\n";
echo dechex(-1); // ffffffffffffffff on 64-bit PHP
?>If you need a signed-looking hex string, handle the sign yourself: ($n < 0 ? '-' : '') . dechex(abs($n)).
Padding and Formatting the Output
Because dechex() strips leading zeros, a single-digit byte like 5 comes back as "5" rather than "05". When you need a fixed width — for example, building a CSS color from RGB components — pad the result:
<?php
$r = 5; $g = 200; $b = 16;
$color = sprintf('#%02s%02s%02s', dechex($r), dechex($g), dechex($b));
echo $color; // #05c810
?>You can also format hex directly with sprintf() using the %x (lowercase) or %X (uppercase) conversion, which is often cleaner when you also want padding:
<?php
echo sprintf('%02X', 255); // FF
?>The Reverse: hexdec()
To go the other way — from a hexadecimal string back to a decimal integer — use hexdec(). The two functions are inverses:
<?php
echo hexdec(dechex(255)); // 255
?>When to Use dechex()
- Generating or inspecting CSS / hex color codes from numeric RGB values.
- Producing hex dumps of binary data alongside
bin2hex(). - Displaying bitmask or flag values in a compact, readable form.
- Logging memory or offset values in the base developers expect.
For other bases, PHP provides matching helpers: decbin() for binary and decoct() for octal, with bindec() and octdec() converting back.
Conclusion
dechex() turns a decimal integer into a lowercase hexadecimal string, with no 0x prefix and no leading zeros. Keep its quirks in mind — floats are truncated, negatives become unsigned — and reach for sprintf('%02x', ...) when you need padding. Pair it with hexdec() whenever you need to round-trip between the two bases.