Here in this post, we are going to see how to convert Binary numbers to Decimal numbers.
we all know that parseInt() can convert any string to integer right? but what most of us don’t know is that there is another optional parameter that we can pass in the function ie., radix, this parameter accepts values between 2 and 32 that represents the number system. Here is the list of number systems
|2||Binary numeral system|
So in our case, we need to use radix as “2” for converting Binary to Decimal.
var binNum = "1100";
var dec = parseInt(binNum, 2);
the above script will give an alert 12 as the decimal value of binary number 1100 is 12.
var hexNum = "C";
var dec = parseInt(hexNum, 16);
Here also you will get an alert 12 as the decimal value of Hexadecimal number C is 12