hexdec
Converts a hexadecimal string to a decimal number.
Syntax
hexdec(string $hex_string): int|floatParameters
| Parameter | Type | Description |
|---|---|---|
| $hex_string | string | A 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, 0Notes
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.