PHP hexdec() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

The hexdec() function converts a hexadecimal (base 16) string into a normal decimal number.

It is the opposite of dechex() and is handy when you read hex values such as colors or IDs.

Syntax

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

PHP
You pass a hex string to hexdec() and it returns the decimal number as an int, or a float if it is very large.
CallWhat it does
hexdec("ff")Returns 255.
hexdec("1A")Returns 26. Uppercase and lowercase letters both work.
hexdec("10")Returns 16.

Pass the digits as a string. A leading 0x prefix is allowed, but other characters such as # should be removed first.

Library declarations
PHP
hexdec(string $hex_string): int|float

Parameters

ParameterDescription
hex_stringRequired. The hexadecimal string that you want to convert.

Return Value

Return ValueDescription
int|floatThe decimal value of the hex string. A float is returned when the number is larger than PHP_INT_MAX.

PHP Examples

Example 1 — Convert hex strings to decimal

Try uppercase and lowercase hex values.

PHP
<?php
echo hexdec("ff") . PHP_EOL;
echo hexdec("1A") . PHP_EOL;
echo hexdec("10") . PHP_EOL;

How It Works

  1. hexdec("ff") is 15 x 16 + 15, which is 255.
  2. hexdec("1A") is 1 x 16 + 10, which is 26. Letter case does not matter.
  3. hexdec("10") is 1 x 16 + 0, which is 16.
  4. Each result is a normal PHP integer you can use in calculations.

Example 2 — Zero and very large values

See what happens when the hex number does not fit into an int.

PHP
<?php
echo var_export(hexdec("0"), true) . PHP_EOL;
echo var_export(hexdec("7fffffffffffffff"), true) . PHP_EOL;
echo var_export(hexdec("ffffffffffffffff"), true) . PHP_EOL;

How It Works

  1. hexdec("0") returns the integer 0.
  2. "7fffffffffffffff" is exactly PHP_INT_MAX on a 64-bit system, so it stays an int.
  3. "ffffffffffffffff" is bigger than PHP_INT_MAX, so PHP switches to a float.
  4. The float loses some precision, so avoid hex values this large when you need exact math.

Example 3 — Split a hex color into RGB

Read the red, green and blue parts out of a CSS color code.

PHP
<?php
$color = "#FF8000";
$hex = ltrim($color, "#");

$red = hexdec(substr($hex, 0, 2));
$green = hexdec(substr($hex, 2, 2));
$blue = hexdec(substr($hex, 4, 2));

echo "Red: " . $red . PHP_EOL;
echo "Green: " . $green . PHP_EOL;
echo "Blue: " . $blue . PHP_EOL;

How It Works

  1. ltrim() removes the # so only valid hex digits are left.
  2. substr() cuts the string into three pairs: FF, 80 and 00.
  3. hexdec() converts each pair into a number from 0 to 255.
  4. The three numbers can now be used for image or color math.

Frequently Asked Questions

It converts a hexadecimal string into a decimal number. For example hexdec("ff") returns 255.
Yes. "FF" and "ff" both return 255.
Not cleanly. The # is ignored but PHP raises a deprecation notice, so remove it first with ltrim($color, "#").
If the number is bigger than PHP_INT_MAX it cannot fit in an int, so PHP returns a float instead.
Use dechex(). For example dechex(255) returns "ff".

Next: hypot()

The hypot() function calculates the length of the hypotenuse of a right-angled triangle wh.

hypot() 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