Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

hexdec

Converts a hexadecimal string to a decimal number.

Syntax

hexdec(string $hex_string): int|float

Parameters

ParameterTypeDescription
$hex_stringstringA string of hexadecimal digits.

Return value

int|floatThe decimal number.

Examples

A colour from #RRGGBB notation:

<?php
 
$color = 'ff8800';
 
echo hexdec(substr($color, 0, 2)), ', ', hexdec(substr($color, 2, 2)), ', ', hexdec(substr($color, 4, 2));

Output:

255, 136, 0

Notes

The # sign and other stray characters must be removed first, for example with ltrim($color, '#').

See also

  • dechex — Converts a number to hexadecimal.
  • bindec — Converts a binary string to a decimal number.
  • base_convert — Converts a number from one base to another.

In the tutorial

  • Numbers and maths — PHP's maths functions — rounding, number formatting, random numbers and the precision of fractions.

Thanks for reading!

Donate to the site — we work hard for you.