dechex():hexadecimal equivalent of decimal number

Convert decimal to hexadecimal

dechex() returns a lowercase hexadecimal representation of an integer.

$number = 255;
echo dechex($number); // ff
<?Php
echo dechex(2); // Output 2 
echo "<br>";
echo dechex(25); // Output 19 
echo "<br>";
echo dechex(200); // Output is c8
echo "<br>";
echo dechex(358); // Output is 166
echo "<br>";
echo dechex(2000); // Output is 7d0
?>
Syntax
string dechex(int $number);
ParameterDESCRIPTION
$numberRequired : Decimal number as Input to convert to equivalent hexadecimal number.
The output is string converted to hexadecimal base system.

Hexadecimal is positional numeral system with base of 16. The chars it used are from 0 to 9 and from A to F.

Example 1: Converting Large Decimal Numbers

<?php
echo dechex(10000);  // Output: 2710
?>

Negative integers and hexadecimal output

Negative integers are represented using the platform's two's-complement integer width, so the exact hexadecimal string differs between 32-bit and 64-bit systems.

<?php
$hex = dechex(-255);
echo $hex; // Platform-dependent two's-complement representation
?>

Example 3: Converting Multiple Decimal Numbers

<?php
$numbers = [15, 255, 4096];
foreach ($numbers as $num) {
    echo dechex($num) . "<br>";
}
?>

Example 4: Converting Decimal to Hexadecimal with Padding

<?php
$number = 255;
echo str_pad(dechex($number), 4, '0', STR_PAD_LEFT);  // Output: 00ff
?>

Example 5: Hexadecimal Conversion in a Loop

<?php
for ($i = 0; $i <= 10; $i++) {
    echo "Decimal: $i => Hex: " . dechex($i) . "<br>";
}
?>

Math Functions base_convert(): Convert number From any base to other bindec(): Decimal equivalent of Binary string decbin() Binary equivalent of decimal number


Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer