Home > Web Front-end > HTML Tutorial > How to convert binary to decimal in JavaScript?

How to convert binary to decimal in JavaScript?

王林
Release: 2023-08-26 14:21:17
forward
2063 people have browsed it

How to convert binary to decimal in JavaScript?

In this tutorial, we will learn to convert binary to decimal in JavaScript. Binary numbers are used in digital electronics. It is a string consisting of '0' and '1', representing a number relative to base 2.

Here are the different ways to convert a binary number to a decimal number.

Use parseInt() method

In JavaScript, the parseInt() method is useful for extracting numbers from strings. We can define the base of a number as a parameter in the parseInt() method.

grammar

Users can use the parseInt() method according to the following syntax to convert binary to decimal.

let binary = "0101";
let output = parseInt(binary, base);
Copy after login

parameter

  • binary - It is a string of binary numbers.

  • base - It is the base of binary strings. In our case we will pass the base "2" so it extracts the binary number from the string and converts it to decimal.

Example

In the example below, we use the parseInt() method. We have passed a binary string and base 2 as arguments to the parseInt() method. We have evaluated our approach on different binary strings. The user can see that we have rendered the decimal output of the binary string.

<html>
<head>
   <title>Convert Binary to decimal in JavaScript.</title>
</head>
<body>
   <h2>Convert Binary to decimal using <i> parseInt() </i> method.</h2>
   <h4>The decimal of 0101 is.</h4>
   <div id = "decimal1"> </div>
   <h4>The decimal of 1100011110001 is.</h4>
   <div id = "decimal2"> </div>
   <script>
      let decimal1 = document.getElementById("decimal1");
      let decimal2 = document.getElementById("decimal2");
      let binary = "0101";
      decimal1.innerHTML = parseInt(binary, 2); // we have given based to extract the binary and convert it into the integer
      binary = "1100011110001";
      decimal2.innerHTML = parseInt(binary, 2);
   </script>
</body>
</html>
Copy after login

Create custom function to convert binary to decimal

In this section, we will create a custom function to convert binary numbers to decimal numbers. We will use the Math.pow() function. Typically, binary numbers are represented as powers of 2s.

Let’s understand it through the following example. ‘110’ is a binary string, we can represent it in decimal as follows.

'110' = 1*22 + 1*21 + 0*20 = 6 (In decimal)
Copy after login

So the user has learned how to convert binary to decimal and we will apply the thing method in the code.

grammar

let decimal = 0;
let l = binary.length;
for (let i = l - 1; i >= 0; i--) {
   if ( binary[i] == '1' )
   decimal += Math.pow( number ,power );// add power of 2 in the decimal if character of binary string is 1.
}
Copy after login

parameter

math.pow() function has two parameters.

  • number - This is the number the user wants to raise to a power. In our case it is fixed, i.e. 2.

  • power - This is the power of the first argument. We will use a for loop to pass different values ​​to get different powers of 2.

Example

In this example, we create a function that converts binary to decimal. Inside the function, we iterate through each character of the binary string starting from the last character.

If we get "1" in a binary string, we will use the Math.pow() function in the decimal variable to add a power of 2 based on the character position. When we get "0" in a binary string, we don't store any value because when we multiply a power of 2 with 0, it returns zero.

<html>
<head>
   <title>Convert Binary to decimal in JavaScript.</title>
</head>
<body>
   <h2>Convert Binary to decimal in JavaScript using the <i> Math.pow() </i> method.</h2>
   <h4>The decimal of 101000 is.</h4>
   <div id = "decimal1"></div>
   <h4>The decimal of 11101010101 is.</h4>
   <div id = "decimal2"></div>
   <script>
      let decimal1 = document.getElementById("decimal1");
      let decimal2 = document.getElementById("decimal2");
      function DecimalToBinary( binary ) {
      let decimal = 0;
      let l = binary.length;
      for (let i = l - 1; i >= 0; i--) {
         if ( binary[i] == '1' )
         decimal += Math.pow( 2, l - 1 - i );
      }
      return decimal;
      }
      decimal1.innerHTML = DecimalToBinary( "101000" );
      decimal2.innerHTML = DecimalToBinary( "11101010101" );
   </script>
</body>
</html>
Copy after login

In this tutorial, we used the parseInt() method to convert binary to decimal. Additionally, we created a custom function. Users can choose one of the methods based on their own understanding.

However, the second method has higher time complexity. So, it's better to use the first method.

The above is the detailed content of How to convert binary to decimal in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template