Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Decimal to Binary and Binary to Decimal Converter

1. ## 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>
<title>onchange base conversion template</title>
<script type="text/javascript">

function validateDigitInBase( digit, base )
{
if()
{
}
}

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>

<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>```

• A google search will give you much simpler functions.

Code:
```<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8" />

<title> Untitled </title>

<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;
}

</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">
<meta charset="UTF-8" />

<title> Untitled </title>

<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) { 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';

</script>

</body>
</html>```

•