Selasa, 28 Agustus 2012

Konversi Bilangan dari Biner ke Oktal PHP


<?php
if (isset($_POST['decimal'])) { //apakah data ter-submit ?
    $decimal = $_POST['decimal'];
    $original = $_POST['decimal'];
    $oktal='';
    if (preg_match('/[^0-9]/',$decimal)) { //memastikan input
        die("Maaf. Inputan salah..");
    }
    else {
        while ($decimal > 0){
        $result=$decimal%8;
        switch($result){ //Looping memutuskan apakah 1 atau 0
        case 0: $oktal.="0"; break;
        case 1: $oktal.="1"; break;
        case 2: $oktal.="2"; break;
        case 3: $oktal.="3"; break;
        case 4: $oktal.="4"; break;
        case 5: $oktal.="5"; break;
        case 6: $oktal.="6"; break;
        case 7: $oktal.="7"; break;
        default:break;
        }
            if ($decimal/8==0) {
                $sisa=($decimal%8);
                $decimal=$sisa;
            }
            else {
                $sisa=($decimal/8);
                $decimal=$sisa%8;
            }}
        $result = strrev($oktal);//hasile diwalik
        echo "Bilangan $original (desimal) dalam hexa adalah $result.
        <a href='konversi_inputan_ke_oktal.php'>Back</a> to the script";
    }  
}
else {
?>
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>"
              method="POST">
            <h3>Masukkan bilangan Desimal disini (cepat!!):
            </h3><input type="text" size="50" name="decimal">
            <input type="submit" value="Konversikan!">
        </form>
        <?php
        echo "<br>",$_SERVER['PHP_SELF'];
        ?>
    </body>
</html>
<?php
}
?>



NB : Cooding ini hanya bisa menampilkan 2 baris saja

Tidak ada komentar:

Posting Komentar