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.

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
Loading...

All contributors

Contribute to Docs

Learn PHP on Codecademy