我想将二进制字符串转换为数字例如
var binary = "1101000" // code for 104
var digit = binary.toString(10); // Convert String or Digit (But it does not work !)
console.log(digit);
这怎么可能?谢谢
我想将二进制字符串转换为数字例如
var binary = "1101000" // code for 104
var digit = binary.toString(10); // Convert String or Digit (But it does not work !)
console.log(digit);
这怎么可能?谢谢
ES6 支持整数的二进制数字文字,所以如果二进制字符串是不可变的,就像问题中的示例代码一样,可以直接输入它,并带有前缀0b
或0B
:
var binary = 0b1101000; // code for 104
console.log(binary); // prints 104
var num = 10;
alert("Binary " + num.toString(2)); // 1010
alert("Octal " + num.toString(8)); // 12
alert("Hex " + num.toString(16)); // a
alert("Binary to Decimal " + parseInt("1010", 2)); // 10
alert("Octal to Decimal " + parseInt("12", 8)); // 10
alert("Hex to Decimal " + parseInt("a", 16)); // 10
parseInt()
基数是最好的解决方案(正如许多人所说的那样):
但是如果你想在没有 parseInt 的情况下实现它,这里有一个实现:
function bin2dec(num){
return num.split('').reverse().reduce(function(x, y, i){
return (y === '1') ? x + Math.pow(2, i) : x;
}, 0);
}
我收集了所有其他人的建议并创建了以下函数,该函数具有 3 个参数,该数字来自的数字和基数以及该数字将位于的基数:
changeBase(1101000, 2, 10) => 104
运行代码片段自己尝试一下:
function changeBase(number, fromBase, toBase) {
if (fromBase == 10)
return (parseInt(number)).toString(toBase)
else if (toBase == 10)
return parseInt(number, fromBase);
else {
var numberInDecimal = parseInt(number, fromBase);
return parseInt(numberInDecimal).toString(toBase);
}
}
$("#btnConvert").click(function(){
var number = $("#txtNumber").val(),
fromBase = $("#txtFromBase").val(),
toBase = $("#txtToBase").val();
$("#lblResult").text(changeBase(number, fromBase, toBase));
});
#lblResult {
padding: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="txtNumber" type="text" placeholder="Number" />
<input id="txtFromBase" type="text" placeholder="From Base" />
<input id="txtToBase" type="text" placeholder="To Base" />
<input id="btnConvert" type="button" value="Convert" />
<span id="lblResult"></span>
<p>Examples: <br />
<em>110, 2, 10</em> => <em>6</em>; (110)<sub>2</sub> = 6<br />
<em>2d, 16, 10</em> => <em>45</em>; (2d)<sub>16</sub> = 45<br />
<em>45, 10, 16</em> => <em>2d</em>; 45 = (2d)<sub>16</sub><br />
<em>101101, 2, 16</em> => <em>2d</em>; (101101)<sub>2</sub> = (2d)<sub>16</sub>
</p>
仅供参考:如果您想2d
作为十六进制数字传递,则需要将其作为字符串发送,如下所示:
changeBase('2d', 16, 10)
function binaryToDecimal(string) {
let decimal = +0;
let bits = +1;
for(let i = 0; i < string.length; i++) {
let currNum = +(string[string.length - i - 1]);
if(currNum === 1) {
decimal += bits;
}
bits *= 2;
}
console.log(decimal);
}
基于@baptx、@Jon和@ikhvjs的评论,以下内容应该适用于非常大的二进制字符串:
// ES10+
function bin2dec(binStr) {
const lastIndex = binStr.length - 1;
return Array.from(binStr).reduceRight((total, currValue, index) => (
(currValue === '1') ? total + (BigInt(2) ** BigInt(lastIndex - index)) : total
), BigInt(0));
}
或者,同样使用for
循环:
// ES10+
function bin2dec(binStr) {
const lastIndex = binStr.length - 1;
let total = BigInt(0);
for (let i = 0; i < binStr.length; i++) {
if (binStr[lastIndex - i] === '1') {
total += (BigInt(2) ** BigInt(i));
}
}
return total;
}
例如:
console.log(bin2dec('101')); // 5n
console.log(bin2dec('110101')); // 53n
console.log(bin2dec('11111111111111111111111111111111111111111111111111111')); // 9007199254740991n
console.log(bin2dec('101110110001101000111100001110001000101000101011001100000011101')); // 6741077324010461213n
为那些希望了解更多信息的人写了一篇关于它的博客文章。
另一个仅用于功能 JS 练习的实现可能是
var bin2int = s => Array.prototype.reduce.call(s, (p,c) => p*2 + +c)
console.log(bin2int("101010"));
+c
强制为类型值以进行正确添加。String
c
Number
稍微修改了传统的二进制转换算法,利用了更多的 ES6 语法和自动功能:
将二进制序列字符串转换为数组(假设它尚未作为数组传递)
反向序列强制 0 索引从最右边的二进制数字开始,因为二进制是从右向左计算的
'reduce' 数组函数遍历数组,对每个二进制位执行 (2^index) 求和 [仅当二进制位 === 1 时](0 位总是产生 0)
注意:二进制转换公式:
{其中d=二进制数字,i=数组索引,n=数组长度-1(从右开始)}
n
∑ (d * 2^i)
i=0
let decimal = Array.from(binaryString).reverse().reduce((total, val, index)=>val==="1"?total + 2**index:total, 0);
console.log(`Converted BINARY sequence (${binaryString}) to DECIMAL (${decimal}).`);