Kamis, 30 Agustus 2012

[TUTORIAL] KONVERSI BILANGAN (full)

Hai hai , kembali lagi sobat .kali ini tentang tutorial cara membuat Konversi Biner , Hexa , Oktal dalam satu  menu . Langsung saja :
========================================================

-->
<?php
error_reporting(0);
$dec = $_REQUEST['dec'];
$option = $_REQUEST['option'];
    function bin($bil){
      $decimal= $bil;
      $asli=$decimal;;
      while ($decimal>0){
                    if($decimal%2 == 0){
                        $binary .= 0;
                        $decimal /= 2;
                    }
                    else{
                        $binary .= 1;
                        $decimal = ($decimal/2)-0.5;

                    }
                }
                $result = strrev($binary);
      return "Biner dari : $asli adalah : $result <br/>";
    }

    function oct($bil){
        $dec= $bil;
        $asli=$dec;
        $oct='';
        while($dec>0){
            $hasil=$dec%8;
                switch($hasil){
                    case 0: $oct.="0"; break;
                    case 1: $oct.="1"; break;
                    case 2: $oct.="2"; break;
                    case 3: $oct.="3"; break;
                    case 4: $oct.="4"; break;
                    case 5: $oct.="5"; break;
                    case 6: $oct.="6"; break;
                    case 7: $oct.="7"; break;
                    default : break;
                }
                if($dec/8==0){
                    $sisa=($dec%8);
                    $dec=$sisa;
                }
                else{
                    $sisa=($dec/8);
                    $dec=$sisa%8;
                }
        }
        $result = strrev($oct);
         return "Octal dari : $ori adalah : $result <br/>";
    }

    function hex($bil){
        $dec= $bil;
        $asli=$dec;
        $hex='';
        while($dec>0){
        $hasil=$dec%16;
            switch($hasil){
                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";
                default : break;
            }
            if($dec/16==0){
                $sisa=($dec%16);
                $dec=$sisa;
            }
            else{
                $sisa=($dec/16);
                $dec=$sisa%16;
            }
            }
         $result = strrev($hex);
         return "Hexa dari : $asli adalah : $result <br/>";
    }
    ?>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Converter </title>
    </head>
    <body>
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>"
                method="post" name="form1">
            <p>Inputkan bilangan desimal :
                <input type="text" name="des" value="<?php echo $dec ?>" /></p>
            <p>Pilih konversi : <br/>
                <input type="radio" name="option" value="bin"
                    <?php ($option=="bin") ? print 'checked=""': print ''; ?>/>
                        Desimal to Biner<br/>
                <input type="radio" name="option" value="hex"
                    <?php ($option=="hex") ? print 'checked=""': print ''; ?>/>
                        Desimal to Hexa<br/>
                <input type="radio" name="option" value="oct"
                    <?php ($option=="oct") ? print 'checked=""': print ''; ?>/>
                        Desimal to Octal<br/>
            </p>
            <input type="submit" name="submit" value="Konversikan"/>
        </form>
        <?php
            if(isset($_POST['des'])){
                $dec = $_REQUEST['des'];
                $option = $_REQUEST['option'];
             
                if($dec=='' || $option==''){
                    echo '<h2>Maaf data Kurang Lengkap</h2>';
                }
                else{
                  
                    switch($option){
                        case 'bin' : echo "<h3>".bin($dec)."</h3>"; break;
                        case 'hex' : echo "<h3>".hex($dec)."</h3>"; break;
                        case 'oct' : echo "<h3>".oct($dec)."</h3>"; break;
                        default : break;
                    }
                }
                echo "<a href='".$_SERVER['PHP_SELF']."'>Reset</a>";
            }
         ?>
    </body>
</html>

 
Ini ScreenShotnya bos :
ini Desimal ke Biner

ini Desimal ke Hexa
ini Desimal ke Oktal

0 komentar:

Poskan Komentar

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Top WordPress Themes