Jumat, 10 Agustus 2012

Contoh Program PHP "konversi decimal to Hexa"

Setelah kemarin ane udah posting contoh php "Decimal to Octal. Sekarang ane mau posting "decimal to HEXA" scriptnya hampirsama kok cima ada beda sedikit. Okay langsung saja lihat script nya dibawah ini :

<?php
if (isset($_POST['decimal'])) {
    $des = $_POST['decimal'];
    $original = $_POST['decimal'];
    $hexa ='';
    if (preg_match('/[^0-9]/',$des)){
        die("Maaf. Inputan yang anda masukkan salah..");
    }


else {
    while($des>0){
        $hasil=$des%16;
        switch($hasil){
            case 0: $hexa.="0"; break;
            case 1: $hexa.="1"; break;
            case 2: $hexa.="2"; break;
            case 3: $hexa.="3"; break;
            case 4: $hexa.="4"; break;
            case 5: $hexa.="5"; break;
            case 6: $hexa.="6"; break;
            case 7: $hexa.="7"; break;
            case 8: $hexa.="8"; break;
            case 9: $hexa.="9"; break;
            case 10: $hexa.="A"; break;
            case 11: $hexa.="B"; break;
            case 12: $hexa.="C"; break;
            case 13: $hexa.="D"; break;
            case 14: $hexa.="E"; break;
            case 15: $hexa.="F";
            default:break;         
        }
if($des/16==0){
    $sisa=($des%16);
    $hexa=$sisa;
}else{
    $sisa=($des/16);
    $des=$sisa%16;
}}
    $result = strrev($hexa);
    echo "Bilangan $original (decimal) dalam Hexa adalah $result.
        <a href='Konversi_DEC_to_HEX.php'>back</a> to the converter";
    }
}
else {
?>
<!DOCTYPE html>
   <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Konversi_DEC_to_HEX</title>
    </head>
    <body>
        <Form action="<?php echo $_SERVER ['PHP_SELF']; ?>"
              method="POST"
              <h3>Masukkan Bilangan Desimal Anda Disini :<br><br>
            </h3><input type="text" size="50" name="decimal" >
              <input type="submit" value="convert now">
        </form>
        <?php
        echo $_SERVER['PHP_SELF'];
        ?>
    </body>
</html>
<?php
}
?>


sekalian aja ane kasih screen shotnya




0 Comments:

Posting Komentar