Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert hex to binary in javascript

Tags:

javascript

I need to convert hex into binary using javascript.

example: 21 23 00 6A D0 0F 69 4C E1 20

should result in: ‭0010000100100011000000000110101011010000000011110110100101001100‬

Does anyone know of a javascript library I might use to accomplish this?

Harriet

like image 705
Harriet Avatar asked Jul 12 '17 09:07

Harriet


People also ask

How to convert hex to binary in js?

Then, for converting, what you do is basicaly getting a string or number, use the parseInt function with the input number value and its base (base 16 for hex here), then you print it to base 2 with the toString function. And finally, you extract the last 8 characters to get your formatted string.

Can hex convert to binary directly?

Hexadecimal to binarySplit the hex number into individual values. Convert each hex value into its decimal equivalent. Next, convert each decimal digit into binary, making sure to write four digits for each value. Combine all four digits to make one binary number.

How do you binary in Javascript?

Users can follow the below syntax to use the toString() method to convert decimal to binary. let decimal = 10; // to convert positive decimal to binary let binary = decimal. toString( redix ); // to convert negative decimal to binary Let binary = (decimal >>> 0). toString( redix );

What is hex in Javascript?

Hex value is also known as Hexadecimal value. It is a specific number system that uses 16 alphanumeric symbols, ranging from 0 to 9, including the letters A to F. In this number system, each value corresponds to the digits 0, 1, 2, 3, 5, 6, 7, 8, 9, A, B, C, D, E, and F.


2 Answers

You can create a function converting a hex number to binary with something like this :

function hex2bin(hex){
    return ("00000000" + (parseInt(hex, 16)).toString(2)).substr(-8);
}

For formatting you just fill a string with 8 0, and you concatenate your number. Then, for converting, what you do is basicaly getting a string or number, use the parseInt function with the input number value and its base (base 16 for hex here), then you print it to base 2 with the toString function. And finally, you extract the last 8 characters to get your formatted string.


2018 Edit :

As this answer is still being read, I wanted to provide another syntax for the function's body, using the ES8 (ECMAScript 2017) String.padStart() method :

function hex2bin(hex){
    return (parseInt(hex, 16).toString(2)).padStart(8, '0');
}

Using padStart will fill the string until its lengths matches the first parameter, and the second parameter is the filler character (blank space by default).

End of edit


To use this on a full string like yours, use a simple forEach :

var result = ""
"21 23 00 6A D0 0F 69 4C E1 20".split(" ").forEach(str => {
  result += hex2bin(str)
})
console.log(result)

The output will be :

00100001001000110000000001101010110100000000111101101001010011001110000100100000

like image 68
Seblor Avatar answered Oct 18 '22 18:10

Seblor


You can use parseInt and toString to change the radix of a number.

function convertNumber(n, fromBase, toBase) {
  if (fromBase === void 0) {
    fromBase = 10;
  }
  if (toBase === void 0) {
    toBase = 10;
  }
  return parseInt(n.toString(), fromBase).toString(toBase);
}
console.log(
  convertNumber("f", 16, 10),
  convertNumber("f", 16, 2),
  convertNumber("1111", 2, 10),
  convertNumber("1111", 2, 16)
);
like image 24
Emil S. Jørgensen Avatar answered Oct 18 '22 19:10

Emil S. Jørgensen