Converts binary numbers to their equivalent decimal numbers.
Binary to Decimal Converter
<script type="text/javascript">
function StringToNumberArray(Bin) {
var numberArray = [];
for (var i = 0; i < Bin.length; i++) {
numberArray.push(Bin.substring(i, i + 1));
}
return numberArray;
}
function ConvertToDecimal(Bin) {
var decimalNumber = 0;
var numberArray = StringToNumberArray(Bin);
numberArray.reverse();
for (var i = 0; i < numberArray.length; i++) {
decimalNumber += numberArray[i] * Math.pow(2, i);
}
return decimalNumber;
}
</script>
Example Usage :
<input type="text" id="txtDec" />
<input type="button" value="Convert" onclick="document.getElementById('spBin').innerHTML=ConvertToDecimal(document.getElementById('txtDec').value);" />
<span id="spBin"></span>
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.