Ini adalah source code cara mengkonversi bilangan decimal ke hexa dengan PHP.
Di sini kita mengimplementasikan penggunaan metode $_POST pada suatu form untuk mendapatkan input. Dan melakukan konversi terhadap inputan tersebut.
Ini source code-nya:
Ini source code-nya:
<?php
if(isset($_POST['decimal'])){
$des=$_POST['decimal'];
$dasli = $des;
if(preg_match("/[^0-9]/", $des)){
echo "Maaf, inputan salah <a href='konversi_dec_to_hex.php'>back</a>";
exit;
}
$hex="";
while($des > 0){
$hasil = $des % 16;
if($des < 1){
$hasil = $des;
$des = 0;
}
$des = $des/16;
switch ($hasil){//di konversikan ke hexa
case 0: $hex.="0"; break;
case 1: $hex.="1"; break;
case 2: $hex.="2"; break;
case 3: $hex.="3"; break;
case 4: $hex.="4"; break;
case 5: $hex.="5"; break;
case 6: $hex.="6"; break;
case 7: $hex.="7"; break;
case 8: $hex.="8"; break;
case 9: $hex.="9"; break;
case 10: $hex.="a"; break;
case 11: $hex.="b"; break;
case 12: $hex.="c"; break;
case 13: $hex.="d"; break;
case 14: $hex.="e"; break;
case 15: $hex.="f"; break;
default:break;
}
}
$result = strrev($hex);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<h3>Konversi Decimal to Hexadecimal</h3>
<form action="Konversi_dec_to_hex.php" method="POST">
<input type="text" name="decimal"/>
<input type="submit" value="Konversikan"/>
</form>
<?php
if($result){
echo "Konversi dari $dasli ke hexadecimal adalah $result";
}
?>
</body>
</html>
if(isset($_POST['decimal'])){
$des=$_POST['decimal'];
$dasli = $des;
if(preg_match("/[^0-9]/", $des)){
echo "Maaf, inputan salah <a href='konversi_dec_to_hex.php'>back</a>";
exit;
}
$hex="";
while($des > 0){
$hasil = $des % 16;
if($des < 1){
$hasil = $des;
$des = 0;
}
$des = $des/16;
switch ($hasil){//di konversikan ke hexa
case 0: $hex.="0"; break;
case 1: $hex.="1"; break;
case 2: $hex.="2"; break;
case 3: $hex.="3"; break;
case 4: $hex.="4"; break;
case 5: $hex.="5"; break;
case 6: $hex.="6"; break;
case 7: $hex.="7"; break;
case 8: $hex.="8"; break;
case 9: $hex.="9"; break;
case 10: $hex.="a"; break;
case 11: $hex.="b"; break;
case 12: $hex.="c"; break;
case 13: $hex.="d"; break;
case 14: $hex.="e"; break;
case 15: $hex.="f"; break;
default:break;
}
}
$result = strrev($hex);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<h3>Konversi Decimal to Hexadecimal</h3>
<form action="Konversi_dec_to_hex.php" method="POST">
<input type="text" name="decimal"/>
<input type="submit" value="Konversikan"/>
</form>
<?php
if($result){
echo "Konversi dari $dasli ke hexadecimal adalah $result";
}
?>
</body>
</html>