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);
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 id="Convert-Binary-to-decimal-using-i-parseInt-i-method">Convert Binary to decimal using <i> parseInt() </i> method.</h2> <h4 id="The-decimal-of-is">The decimal of 0101 is.</h4> <div id = "decimal1"> </div> <h4 id="The-decimal-of-is">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>
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)
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. }
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 id="Convert-Binary-to-decimal-in-JavaScript-using-the-i-Math-pow-i-method">Convert Binary to decimal in JavaScript using the <i> Math.pow() </i> method.</h2> <h4 id="The-decimal-of-is">The decimal of 101000 is.</h4> <div id = "decimal1"></div> <h4 id="The-decimal-of-is">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>
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service
