<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>
1
Contributor
0
Replies
1
Views
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.