Binary to number in javascript

Feb 11, 2022

A binary number is a number expressed in base-2, as opposed to conventional decimal numbers in base-10.

Below is a live calculator.

BinaryDecimal

We also have a tool to convert decimal to binary.

How the Calculator Works

Converting binary numbers to decimal is easy. For example, let x = '101010' creates a new variable x that contains the number as a string 101010. JavaScript has a parseInt[] method that takes a binary and radix parameter, and returns a number. Calling parseInt[binary, radix] tells JavaScript to convert binary to a number containing the decimal representation of 101010. If binary is not a string, it will be converted to one using the toString[] function.

let x = '101010';

parseInt[x, 2] // 42

The toString[] method also handles non-integers and negative numbers. For example:

x = -101010
parseInt[x, 2]; // -42

x = 101010.101010;
parseInt[x, 2]; // 42

No Technology Method

How do you quickly convert 101010 to 42 without a computer? It takes some practice to make it easy, but here's a few ways to convert a binary string to a decimal number. One method discussed is doubling, described below:

  • Take the binary string.
  • Starting from the left, double your previous total and add the current digit.
  • Double your current total and add the next leftmost digit.
  • Repeat the previous step until you have gone through the entire string.

Below is a JavaScript function that implements the above procedure:

function toDecimal[v] {
  let binary = '';
  if[typeof v == 'string'] {
    binary = v.split[];
  } else {
      binary = v.toString[].split[];
  }
  let decimal = 0;
  for[let i = 0; i < binary.length; i++] {
      decimal = [decimal * 2] + binary[i];
  }
  return decimal;
}

Here's what the procedure looks like with 101010:

  1. 101010
  2. 101010 => 0 + 1 == 1
  3. 1 * 2 + 0 == 2
  4. 2 * 2 + 1 == 5
  5. 5 * 2 + 0 == 10
  6. 10 * 2 + 1 == 21
  7. 21 * 2 + 0 == 42

More Tools

  • Format CSS in the Browser
  • Convert JSON to YAML

This will take a buffer hex and convert it to a binary str and back to the buffer hex.

NOTE: when I say a buffer hex, I mean a decimal value because when you iterate over a buffer and pull each item in the array, it gives you the decimal value [eg: 210, instead of d2].

var buffer - new Buffer[[0, 210, 242]]; // Node

// var arrayBuffer = new ArrayBuffer[3]; // JavaScript

// var uint8 = new Uint8Array[arrayBuffer]; // JavaScript/ 16Array, 32Array, etc

Need to be acquainted with buffers

You'll iterate over the buffer with a for[]{} and inside you can do something like:

[210].toString[2]; // '11010010'

[210].toString[16]; // 'd2' [untested]

[210].toString[8]; // [Octal-Digits representation]

parseInt[[210].toString[2], 2]; // 210

parseInt[[210].toString[2], 2].toString[16]; // 'd2'

Obviously, instead of using "[210].toString[2]" IN YOU FOR LOOP, you would use "[buffer[i]].toString[2]"

Endian Rep is up to you! :] [array.reverse[]]

Hope this helps!

PS. parseInt[['00000' + [210].toString[2].substring[5, 8]], 2]; // 2

parseInt[[210].toString[2].substring[5, 8], 2]; // 2

Let’s say, we have an array of Numbers that contains 0 and 1 −

const arr = [0, 1, 0, 1];

We are required to write an Array function, toBinary[] that returns the corresponding binary for the array it is used with.

For example − If the array is −

const arr = [1, 0, 1, 1];

Then the output should be 11 because the decimal representation of binary 1011 is 11.

Therefore, let’s write the code for this function.

Method 1: Using library methods

In JavaScript, there exists a method parseInt[], that takes in two arguments first one is a string and second a number that represents a particular base, like 10 for decimal base, 2 for binary. This function parses the string argument and returns an integer of the specified radix [ base].

In our case, to convert the array of binary to decimal, we can use the parseInt[] function like this −

const arr = [1, 0, 1, 1];
const parseArray = arr => {
   const binaryString = arr.join[""];
   return parseInt[binaryString, 2];
};
console.log[parseArray[arr]];

Method 2: Reducing the array

In this method we iterate over the binary array and construct a decimal based on the corresponding binary numbers. We will use the left shift operator [ {       return [acc

Chủ Đề