PHP hexdec()

ChrisLarham's avatar
Published Jul 12, 2023
Contribute to Docs

The hexdec() function returns the decimal equivalent of a given hexadecimal string. It ignores any non-hexadecimal characters, computing the result as if they did not exist.

The function returns a value of type int if the converted number fits into the platform’s int type; otherwise, a float is returned.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours

Syntax

$decimal = hexdec(hexString);

Where hexString is the hexadecimal string that is to be converted into a decimal.

Example

This example converts the hexadecimal string "CDECADE" into its decimal equivalent.

<?php
$decimal = hexdec("CDECADE");
echo $decimal;
?>

This example results in the following output:

215927518

Codebyte Example

This example returns the value of both a well-formed hexadecimal string and its malformed equivalent (non-hexadecimal characters ignored):

Code
Output

All contributors

Contribute to Docs

Learn PHP on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours