dechex
Converts a number to hexadecimal.
Syntax
dechex(int $num): stringParameters
| Parameter | Type | Description |
|---|---|---|
| $num | int | A non-negative integer. |
Return value
stringThe hexadecimal form of the number.
Examples
A number to hexadecimal and back:
<?php
echo dechex(255), ' ', hexdec('ff');Output:
ff 255A color from three components:
<?php
echo sprintf('#%02x%02x%02x', 124, 58, 237);Output:
#7c3aedNotes
dechex() adds no leading zero: dechex(10) gives a, not 0a. sprintf('%02x', $value) is handier for colors.
See also
- base_convert — Converts a number from one base to another.
- sprintf — Returns a string built from a format template.
Related recipes
In the tutorial
- Numbers and maths — PHP's maths functions — rounding, number formatting, random numbers and the precision of fractions.