dechex
Converts a decimal integer to its lowercase hexadecimal string representation.
dechex(int $num): string- Added in PHP 4.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$num | int | The decimal value to convert. PHP's int is signed but dechex() treats the value as unsigned, so negative integers wrap around. |
Return value
stringA lowercase hexadecimal string representing $num as an unsigned value, with no 0x prefix and no leading zeros.
Errors
Throws a TypeError if $num is not an int or a value that can be coerced to int.
Changelog
- PHP 8.1Passing a float that loses precision in the implicit int conversion emits a deprecation notice; cast explicitly with (int).
dechex() converts a decimal integer to base 16 and returns it as a lowercase string with no 0x prefix and no leading zeros. Its inverse is hexdec().
When to reach for it
Colour handling is the everyday case: turning RGB components into a CSS hex triplet. It is also the readable way to print byte values, checksums, memory offsets, protocol identifiers and any other value people habitually read in hex.
What trips people up
The biggest surprise is that dechex() treats its argument as unsigned. Negative integers wrap to the top of the range instead of gaining a minus sign, so dechex(-1) is ffffffffffffffff on a 64-bit build. The largest value it can convert is PHP_INT_MAX * 2 + 1.
The second surprise is missing padding. dechex(10) is "a", one character, so building a colour by concatenating three calls produces a broken string when any component is below 16. Use sprintf('%02x', $n), or sprintf('#%02x%02x%02x', $r, $g, $b) for a whole colour, and let the formatter handle width for you. For uppercase output use %02X or wrap the call in strtoupper().
Example
When to use it
- Build a CSS colour string such as #ff6347 from separate red, green and blue values.
- Print byte values or a checksum in hex when logging binary protocol data.
- Render an integer ID in a compact hex form for URLs or short codes.
Frequently asked questions
How do I get two-digit hex output from dechex?
dechex() never pads, so use sprintf('%02x', $num) instead, or str_pad(dechex($num), 2, '0', STR_PAD_LEFT). This matters for colour codes, where dechex(10) alone would produce a 5-character CSS value.
How do I make dechex return uppercase hex?
Wrap it in strtoupper(), or skip dechex() entirely and use sprintf('%X', $num) which formats in uppercase directly.
Why does dechex return ffffffffffffffff for -1?
dechex() converts unsigned values, so a negative int is reinterpreted as its two's complement bit pattern. If you need a signed representation, format the sign yourself: ($n < 0 ? '-' : '') . dechex(abs($n)).
More examples
RGB to hex and back again
The green channel is 128, which is 80 in hex, and blue is ff, giving #0080ff. Splitting the string back into two-character chunks and mapping hexdec() over them recovers the original 0, 128, 255 exactly.
<?php
function toHex(int $r, int $g, int $b): string {
return sprintf('#%02x%02x%02x', $r, $g, $b);
}
$hex = toHex(0, 128, 255);
echo $hex, "\n";
[$r, $g, $b] = array_map('hexdec', str_split(substr($hex, 1), 2));
echo "rgb($r, $g, $b)\n";Hex dump of a short string
Both lines encode the same bytes; the manual loop shows why str_pad is needed, since the space character is 0x20 and the digit 8 is 0x38. For real byte-to-hex work, bin2hex() does the whole string in one call and is much faster.
<?php
$data = "PHP 8.5";
$out = [];
foreach (str_split($data) as $ch) {
$out[] = str_pad(dechex(ord($ch)), 2, '0', STR_PAD_LEFT);
}
echo implode(' ', $out), "\n";
echo bin2hex($data), "\n";Unsigned wraparound
dechex(-1) gives 16 f characters on a 64-bit build because the value is read as unsigned. Feeding that back through hexdec() cannot fit in an int, so it returns the float 1.8446744073709552E+19 and the identity check is false — negatives do not round-trip.
<?php
var_dump(dechex(-1));
var_dump(dechex(PHP_INT_MAX));
var_dump(hexdec(dechex(-1)) === -1);
var_dump(hexdec(dechex(-1)));
Discussion