Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

dechex

Converts a number to hexadecimal.

Syntax

dechex(int $num): string

Parameters

ParameterTypeDescription
$numintA 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 255

A color from three components:

<?php
 
echo sprintf('#%02x%02x%02x', 124, 58, 237);

Output:

#7c3aed

Notes

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.

Thanks for reading!

Donate to the site — we work hard for you.