SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    Passionate Web Developer Egyptechno's Avatar
    Join Date
    Jan 2004
    Location
    Dubai
    Posts
    259
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Decimal >> (Binary/Octal/Hexa) PHP Converter

    I've just finished this converter ..

    converts any numbers from decimal to base 2 or 8 or 16 ..

    I called it " Unique Machine Language Converter = UMLC " ..

    I know .. it's not so useful for using .. but I just wanted to do it !

    have a look :
    http://www.egyptechno.com/umlc/?lang=en


    for those who need a look on source code :
    PHP Code:
    <html>
    <title>Unique Machine Language Converter</title>
    <div align='center'>
    <table border='1' cellpadding='1' cellspacing='3' style="border-collapse: collapse; border: 1px dashed #004080; background-color: #F2F2F2" bordercolor="#111111"  width='35%' id='AutoNumber2' bgcolor='#FFF2E6' bordercolorlight='#FF9900' bordercolordark='#FF9900' bordercolor='#FF9900'>
    <tr><td  align='center'>
    <img src='umlc.gif'>
    <br>

    <form method='post'>
    <input type='text' name='deci' style='border: 1px dashed #C1C1C1; background-color: #FFF'"> 
    <select name='base'>
    <option value='2' selected>Binary</option>
    <option value='8'>Octal</option>
    <option value='16'>Hexa</option>
    </select>
    <input type='submit' name='ConverIt' value='Convert'>
    </form>

    <?
    if ($_POST['ConverIt']){
        
    // function to check intgers
    function check_no($no) {
        return (
    preg_match(
            
    '/^[0-9]+$/',$no));
    }
    // -------------------------

    // function to check abnormal hexa numbers
    function check_hexa($value){
        if (
    $value == 10){
            return 
    "A";
        }
        if (
    $value == 11){
            return 
    "B";
        }
        if (
    $value == 12){
            return 
    "C";
        }
        if (
    $value == 13){
            return 
    "D";
        }
        if (
    $value == 14){
            return 
    "E";
        }
        if (
    $value == 15){
            return 
    "F";
        }
        
    }
    // -----------------------------------------



    if ($_POST['deci'] !== ""){
    if (
    check_no($_POST['deci']) == true){
        
    echo 
    "<table dir='rtl' border='1' cellpadding='1' cellspacing='3' style='border-collapse: collapse; border: 1px dashed #004080; background-color: #F2F2F2' bordercolor='#111111'  width='35%' id='AutoNumber2' bgcolor='#FFF2E6' bordercolorlight='#FF9900' bordercolordark='#FF9900' bordercolor='#FF9900'><tr>";
    for (
    $i=$_POST[deci];$i>=1;){
        echo 
    "<td align='right'><span lang='en-us'>";
        
        
    // Divide by the base
        
    $i=$i/$_POST[base];
        
        
    // check points
        
    if (preg_match("/[0-9].[0-9]/",$i)){
            
        
    // break
        
    $a explode (".",$i);
        
    $bit ".$a[1]";
        
        
    // * base to get the reminder
        
    $bit $bit*$_POST[base];
        
        
    // it's my digit !
        // but check first for hexa
        
    if (check_hexa($bit) == False){
            echo 
    $bit;
        }Else{
            echo 
    check_hexa($bit);
        }
            


        
    // and let's continue
        
    $i=$a[0];
        
        
    // else, no points .. it's Sa7ee7
        
    }Else{
    echo 
    0;
            
        }
        echo 
    "</span></td>";
    }

    }Else{
        echo 
    "Just Numbers dude ^_^";
    }

    }Else{
        echo 
    "Ok, just input any number !";
    }
    }



  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  3. #3
    SitePoint Wizard
    Join Date
    May 2003
    Location
    Berlin, Germany
    Posts
    1,829
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should wrap the functionality into an Object, so one could use it like that:

    PHP Code:
    <?php
    $conv
    =new Converter;
    echo 
    $conv->convertDecimalToBinary('16');
    ?>
    @stereofrog: He said he just wanted to do it as an exercise.

  4. #4
    SitePoint Guru thr's Avatar
    Join Date
    Jun 2003
    Location
    Sweden
    Posts
    664
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This isn't realy "Application Design", now is it ?

  5. #5
    SitePoint Evangelist ghurtado's Avatar
    Join Date
    Sep 2003
    Location
    Wixom, Michigan
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Let's start a new PHP forum: "Custom Rewrites of built-in PHP functionality". (jk)
    Garcia

  6. #6
    Passionate Web Developer Egyptechno's Avatar
    Join Date
    Jan 2004
    Location
    Dubai
    Posts
    259
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)


    I didn't know about that function

    anyway .. was just a try !

    successfull try

    Thanks all !

  7. #7
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ghurtado
    Let's start a new PHP forum: "Custom Rewrites of built-in PHP functionality". (jk)
    Actually, rewrting some of built-in functions could be a good exercise. Enterprise patterns and application design are important things... but "you always return to the basics(tm)"

  8. #8
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Egyptechno, don't take these guys the wrong way. It's sometimes brutal in the Advanced Forum, but you hacked up some nice code and got real feedback. We are all here to learn.
    Christopher

  9. #9
    Passionate Web Developer Egyptechno's Avatar
    Join Date
    Jan 2004
    Location
    Dubai
    Posts
    259
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    arborint :

    Thanks


    wait from me the next rewrite function after few days


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •