二进制转十进制
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script>
var num = prompt("请输入一个二进制");
num = parseInt(num);
//获取二进制有几位
var counter = 0;
var digital = 1;
var tempNum = num;
while (tempNum > 0) {
tempNum = parseInt(tempNum / 10);
counter++;
digital *= 10;
}
//计算为十进制
var decimal = 0;
while (counter > 0) {
counter--;
//计算2的几次方
var temp = 1;
var i = counter;
while (i > 0) {
temp *= 2;
i--;
}
//计算最终结果
digital /= 10;
decimal += temp * (parseInt(num / digital) % 10);
}
alert(decimal);
</script>
</head>
<body>
</body>
</html>