JavaScript二进制转十进制代码
<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>