How to Manually Convert a Binary String Into a Decimal Number in JavaScript?

Calculate binary string's decimal equivalent in JavaScript

If you have a really large binary string, or if you simply do not wish to use Number.parseInt() to convert binary string into its decimal equivalent, then you may manually convert it in the following ways:

To learn about the logic behind the conversion in either of the methods above, make sure you read about the binary to decimal conversion formula.

Using BigInt and Array.prototype.reduceRight()

To convert large binary strings into decimal numbers you can use the Array.prototype.reduceRight() method (introduced in ES5) along with BigInt (introduced in ES10) like so:

// 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));
}

This is equivalent to using Array.prototype.reverse() with Array.prototype.reduce() method, for example, like so:

// ES10+
function bin2dec(binStr) {
    return Array.from(binStr).reverse().reduce((total, currValue, index) => (
        (currValue === '1') ? total + (BigInt(2) ** BigInt(index)) : total
    ), BigInt(0));
}

With either variation of the function, you would get the same result. For example:

console.log(bin2dec('101')); // 5n
console.log(bin2dec('101101')); // 45n
console.log(bin2dec('11111111111111111111111111111111111111111111111111111')); // 9007199254740991n
console.log(bin2dec('101110110001101000111100001110001000101000101011001100000011101')); // 6741077324010461213n

To learn about the logic behind the conversion, make sure you read about the binary to decimal conversion formula.

Using BigInt and for Loop

If you wish to write a more performant function than using Array.prototype.reduceRight(), then the following might help:

// 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;
}

As an alternative, you may use reversed binary string array instead of relying on the "lastIndex", like so:

// ES10+
function bin2dec(binStr) {
    const binArr = Array.from(binStr).reverse();
    let total = BigInt(0);

    for (let i = 0; i < binArr.length; i++) {
        if (binArr[i] === '1') {
            total += (BigInt(2) ** BigInt(i));
        }
    }

    return total;
}

With either variation of the function, you would get the same result. For example:

console.log(bin2dec('101')); // 5n
console.log(bin2dec('101101')); // 45n
console.log(bin2dec('11111111111111111111111111111111111111111111111111111')); // 9007199254740991n
console.log(bin2dec('101110110001101000111100001110001000101000101011001100000011101')); // 6741077324010461213n

To learn about the logic behind the conversion, make sure you read about the binary to decimal conversion formula.

What Is the Binary to Decimal Conversion Formula?

Let's suppose you have a binary number 101101 and you wish to convert it into its decimal equivalent (which is 45 in this case). To do that, the first step is to list out (sequentially incremented) powers of 2 for all the binary number digits starting from the right-hand side. For example:

 1     0     1     1     0     1
 |     |     |     |     |     |
2^5   2^4   2^3   2^2   2^1   2^0

Now, you simply need to multiply each binary digit with its corresponding power of 2 that you listed in the last step:

  1          0          1          1          0          1
  |          |          |          |          |          |
1 * 2^5    0 * 2^4    1 * 2^3    1 * 2^2    0 * 2^1    1 * 2^0

Finally, you simply need to calculate the sum all the products obtained from the last step:

  1           0           1           1           0           1
  |           |           |           |           |           |     =     45
1 * 32   +  0 * 16   +  1 * 8   +   1 * 4   +   0 * 2   +   1 * 1

This is known as the positional notation method.


Hope you found this post useful. It was published . Please show your love and support by sharing this post.