hexdec()
Today, we will discuss the hexdec() function in PHP. This function is used to convert a hexadecimal number to its decimal equivalent.
The PHP hexdec() function converts a hexadecimal (base-16) string into its decimal (base-10) equivalent. It is the inverse of dechex(), and it is handy whenever you read hex values that come from colors, byte dumps, hashes, or low-level protocols and need to do arithmetic on them.
This page covers the syntax, what the return type really is, how hexdec() handles invalid characters, the large-number gotcha, and how it relates to the other base-conversion functions.
Syntax
hexdec(string $hex_string): int|float$hex_string— the hexadecimal string to convert. Hex digits are0-9anda-f(case-insensitive, soFFandffare equal).- Return value — the decimal value as an
int, or afloatwhen the number is too large to fit in a PHP integer.
A Basic Example
1a in base-16 means 1 × 16 + 10 = 26, which is the value printed. The input is a string here — that is the normal and recommended way to pass hex values, because a literal like 1a is not valid PHP and 0x1a would already be an integer.
Converting Several Values
hexdec() is case-insensitive and ignores any leading 0x prefix that you might copy from another tool:
<?php
echo hexdec("ff"), "\n"; // 255
echo hexdec("FF"), "\n"; // 255 (same — case does not matter)
echo hexdec("100"), "\n"; // 256
echo hexdec("7fff"), "\n"; // 32767
echo hexdec("0x1A"), "\n"; // 26 (the "0x" is ignored)
?>A common real-world use is splitting a CSS hex color into its red, green, and blue channels:
<?php
$color = "ff8800";
$red = hexdec(substr($color, 0, 2)); // 255
$green = hexdec(substr($color, 2, 2)); // 136
$blue = hexdec(substr($color, 4, 2)); // 0
echo "rgb($red, $green, $blue)"; // rgb(255, 136, 0)
?>How Invalid Characters Are Handled
hexdec() does not throw an error on a malformed string. Instead, every character that is not a valid hex digit is silently ignored (and, since PHP 7.4, a deprecation/E_WARNING notice is emitted). The remaining valid digits are still converted:
<?php
echo hexdec("a0.5"), "\n"; // 2565 — the "." is dropped, so "a05" is converted
echo hexdec("xyz1f"), "\n"; // 31 — only "1f" is valid hex
?>Because the failure is silent, validate input yourself when it might be untrusted. ctype_xdigit() checks that a string contains only hex digits:
<?php
$input = "a0.5";
if (ctype_xdigit($input)) {
echo hexdec($input);
} else {
echo "Not a valid hexadecimal string";
}
// Not a valid hexadecimal string
?>Large Numbers Return a Float
PHP integers are platform-bound (64-bit on most modern systems). When a hex value exceeds PHP_INT_MAX, hexdec() returns a float instead of an int so the value is not truncated — though very large floats lose precision:
<?php
var_dump(hexdec("1a")); // int(26)
var_dump(hexdec("ffffffffffffffff")); // float(1.8446744073709552E+19)
?>If you need exact arithmetic on huge hex numbers, reach for the BCMath or GMP extensions instead.
Related Base-Conversion Functions
hexdec() is one of a family of base converters. Pick the one that matches your input and output bases:
| Function | Converts from | Converts to |
|---|---|---|
hexdec() | hexadecimal | decimal |
dechex() | decimal | hexadecimal |
bindec() | binary | decimal |
octdec() | octal | decimal |
For arbitrary bases (anything from base-2 to base-36) use base_convert(), and to turn a hex string into raw bytes use hex2bin().
Conclusion
hexdec() turns a hexadecimal string into a decimal number, returning an int (or a float for very large values). Remember that it ignores invalid characters silently — so validate untrusted input with ctype_xdigit() first — and use its counterpart dechex() to convert back.