82. PHP Program to Convert Decimal number into Hexadecimal number.
How does this program work?
- This program is used to convert given Decimal number into equivalent of Hexadecimal number using PHP.
- That means convert number with base value 10 to base value 16.
- The integer entered by user will store in one variable. Divide that number by 16.
- Store the remainder when the number is divided by 2 in an array.
- If the remainder will less than 10 then we can add 48 to the remainder else we can add 55 to the remainder.
Here is the code
<html>
<head>
<title>PHP Program To convert Decimal number into Hexadecimal number</title>
</head>
<body>
<form method="post">
<table border="0">
<tr>
<td> <input type="text" name="num" value="" placeholder="Enter decimal number"/> </td>
</tr>
<tr>
<td> <input type="submit" name="submit" value="Submit"/> </td>
</tr>
</table>
</form>
<?php
if(isset($_POST['submit']))
{
$n = $_POST['num'];
$num = $n;
$i = 0;
$hexadecimal;
while($n>0) {
$rem = 0; //Initially 0 stored in remainder
$rem = $n%16;
if($rem <10) //If remainder is less than 10 then 48 to add remainder
//Else 55 to add remainder
{
$hexadecimal[$i] = chr($rem + 48);
$i++; //Count will increment
}
else
{
$hexadecimal[$i] = chr($rem + 55);
$i++;
}
$n = (int) ($n/16);
}
echo "The Decimal number is: ".$num." " ;
echo "The Equivalent Hexadecimal number is : ";
for($j = $i-1; $j>=0; $j--)
echo $hexadecimal[$j];
return 0;
}
?>
</body>
</html>