dechex():hexadecimal equivalent of decimal number

<?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
?>

Example 2: Converting Negative Numbers

<?php
echo dechex(-255);  // Output: ffffff01 (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


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com











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