How to convert binary string to decimal?

I want to convert binary string in to digit E.g

var binary = "1101000" // code for 104
var digit = binary.toString(10); // Convert String or Digit (But it does not work !)
console.log(digit);

How is it possible? Thanks

Answer

The parseInt function converts strings to numbers, and it takes a second argument specifying the base in which the string representation is:

var digit = parseInt(binary, 2);

See it in action.

Leave a Reply

Your email address will not be published. Required fields are marked *