Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Nov 2013
    Location
    Minnesota
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Decimal to Binary and Binary to Decimal Converter

    Hello,

    I am new to programming and was giving the assignment below. I've spent several hours unsuccessfully trying to understand the code below and to fill in the correct information to get the code to work. If anyone could help me understand this I would greatly appreciate it.

    I understand the concept and the math involved with converting to base but I am having trouble correlating this with the actual code.



    Code:
    ]<html>
        <head>
            <title>onchange base conversion template</title>
            <script type="text/javascript">
    
                function validateDigitInBase( digit, base )
                {
                    if()
                    {
                        alert();
                    }
                }
    
                function convertBinaryToDecimal( binaryValue )
                // Assumes that binaryValue is a decimal value where each decimal digit represents a binary digit
                {
    
                    // constants
                    var SOURCE_BASE      =  2;
                    var DESTINATION_BASE = 10;
                    var DISPLAY_BASE     = 10;
    
                    // local variables
                    var placeValue   = 1; // initialized to the multiplicative identity
                    var decimalValue = 0; // initialized to the additive identity
                    var digit;
    
                    digit = ;                  // extracting the rightmost DISPLAY_BASE (simulated binary) digit
                    validateDigitInBase( , );  // ensuring that the new digit is valid in the SOURCE_BASE
                    decimalValue += ;          // adding the new digit scaled by its SOURCE_BASE place value
                    placeValue *= ;            // updating the SOURCE_BASE place value
                    binaryValue = ;            // the decimal (simulated binary) value without the rightmost DISPLAY_BASE digit
                    // repeat the above code block seven more times
    
                    return decimalValue;
                }
    
                function convertDecimalToBinary( decimalValue )
                {
    
                    // constants
                    var SOURCE_BASE      = 10;
                    var DESTINATION_BASE =  2;
                    var DISPLAY_BASE     = 10;
    
                    // local variables
                    var placeValue  = 1; // initialized to the multiplicative identity
                    var binaryValue = 0; // initialized to the additive identity
                                         // Assumes sthat binaryValue is a decimal value where each decimal digit represents a binary digit
                    var digit;
    
                    digit = ;                  // extracting the rightmost DESTINATION_BASE digit
                    validateDigitInBase( , );  // ensuring that the new digit is valid in the DESTINATION_BASE
                    binaryValue += ;           // adding the new digit scaled by its DISPLAY_BASE place value
                    placeValue *= ;            // updating the DISPLAY_BASE place value
                    decimalValue = ;           // the decimal value without the rightmost DESTINATION_BASE digit
                    // repeat the above code block seven more times
    
                    return binaryValue;
                }
    
            </script>
        </head>
    
        <body>
            <h1><tt>onchange</tt> base conversion template</h1>
           <h2>Click to copy either value</h2>
           <input type="text" id="box1" size="15" value="1" onchange="document.getElementById('box2').value = 
                          convertBinaryToDecimal( parseFloat(document.getElementById('box1').value) );">
           &nbsp; <tt>&lt;–––-&gt;</tt> &nbsp;
           <input type="text" id="box2" size="15" value="1" onchange="document.getElementById('box1').value = 
                          convertDecimalToBinary( parseFloat(document.getElementById('box2').value) );">
        </body>
    </html>

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,065
    Thanks
    36
    Thanked 497 Times in 491 Posts
    A google search will give you much simpler functions.

    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    
    <title> Untitled </title>
    
    </head>
    <body>
    <script type="text/javascript">
    // Modified from: http://www.name-generators.com/javascript-2/how-to-make-binary-decimal-conversion-in-javascript.htm
    
    function convertBinToDec(v) {
       var binaryNumber = v;
       binaryNumber = parseInt(binaryNumber,2);
       var decimalNumber = binaryNumber.toString(10);
       return decimalNumber;
    }
    
    function convertDecToBin(v) {
       var decimalNumber = v;
       decimalNumber = parseInt(decimalNumber);
       var binaryNumber = decimalNumber.toString(2);
       return binaryNumber;
    }
    
    alert(convertBinToDec('11111111'));
    alert(convertDecToBin(255));
    
    </script>
    
    </body>
    </html>
    Can be made even simpler as a function
    or written using a .prototype. if necessary

    Compressed examples:
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    
    <title> Untitled </title>
    
    </head>
    <body>
    <script type="text/javascript">
    // Modified from:
    //   http://www.name-generators.com/javascript-2/how-to-make-binary-decimal-conversion-in-javascript.htm
    //   Additional comments and condensation of code
    
    function convertBinToDec(v) { return parseInt(v,2).toString(10); }
    
    function convertDecToBin(v) { return Number(v).toString(2); }
    
    var str  = 'passing value as string:\n';
        str += '11111111 binary is '+convertBinToDec('11111111')+' decimal\n';
        str += '255 decimal is '+convertDecToBin('255')+' binary\n\n';
    
        str += 'passing value as integer number value:\n';
        str += '1111 binary is '+convertBinToDec(1111)+' decimal\n';
        str += '127 decimal is '+convertDecToBin(127)+' binary';
    
    alert(str);
    
    </script>
    
    </body>
    </html>
    Last edited by jmrker; 11-07-2013 at 04:02 PM. Reason: Further compression of code


  •  

    Tags for this Thread

    Posting Permissions

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