'convert hex to binary in 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
Solution 1:[1]
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)
);
Solution 2:[2]
Unfortunately, previous answers seem not to be working with very large values (e.g., 512 bits so common in cryptography). This solution may be a bit slower, but it guarantees dealing with input of any length.
function hex2bin(hex){
hex = hex.replace("0x", "").toLowerCase();
var out = "";
for(var c of hex) {
switch(c) {
case '0': out += "0000"; break;
case '1': out += "0001"; break;
case '2': out += "0010"; break;
case '3': out += "0011"; break;
case '4': out += "0100"; break;
case '5': out += "0101"; break;
case '6': out += "0110"; break;
case '7': out += "0111"; break;
case '8': out += "1000"; break;
case '9': out += "1001"; break;
case 'a': out += "1010"; break;
case 'b': out += "1011"; break;
case 'c': out += "1100"; break;
case 'd': out += "1101"; break;
case 'e': out += "1110"; break;
case 'f': out += "1111"; break;
default: return "";
}
}
return out;
}
Solution 3:[3]
The simplest implementation
const hex2bin = (data) => data.split('').map(i =>
parseInt(i, 16).toString(2).padStart(4, '0')).join('');
Solution 4:[4]
This work for me.
function hex2bin(hexSource) {
var bin = '';
for (var i=0;i<hexSource.length;i=i+2) {
bin += String.fromCharCode(hexdec(hexSource.substr(i,2)));
}
return bin;
}
function hexdec(hexString) {
hexString = (hexString + '').replace(/[^a-f0-9]/gi, '')
return parseInt(hexString, 16)
}
Solution 5:[5]
this might be faster method, concept is considering integer's capacity we can divide hex string into blocks of 8 chars instead of 1 char:
function hexToBinary(hex) {
var binary = "";
var remainingSize = hex.length;
for (var p = 0; p < hex.length/8; p++) {
//In case remaining hex length (or initial) is not multiple of 8
var blockSize = remainingSize < 8 ? remainingSize : 8;
binary += parseInt(hex.substr(p * 8, blockSize), 16).toString(2).padStart(blockSize*4,"0");
remainingSize -= blockSize;
}
return binary;
}
Solution 6:[6]
I agree for hex to binary going character by character isn't the fastest or most efficient, but I think it's difficult to do that and still have readable code. I think these two are good starting points for those less familiar.
function hex2bin(hex) {
let bin = "";
let bitsInHex = 4;
Array.from(hex).forEach(
function (char) {
let currentBin = parseInt(char, 16).toString(2);
if (currentBin.length < bitsInHex) {
let padding = "0".repeat(bitsInHex-currentBin.length);
currentBin = padding + currentBin;
}
bin += currentBin;
}
);
return bin;
}
function bin2hex(bin) {
let hex = "";
let bitsInHex = 4;
for (let i = 0; i < bin.length; i = i + bitsInHex) {
let eightBits = bin.substr(i, bitsInHex);
let currentHex = (parseInt(eightBits, 2)).toString(16).toUpperCase();
hex += currentHex;
}
return hex;
}
Solution 7:[7]
For some reason, Javascript gives only 0000 if the character length is greater than 12. I am self-taught JS, so don't know the reason.
But I have a workaround for this problem. Just divided the original string into an array of smaller chunks, and then find their binary value.
function hex2bin(hex) {
let chunks = hex.match(/.{1,8}/g), bin = ''
chunks.forEach(c => (bin += parseInt(c, 16).toString(2)))
bin = bin.padStart(4, '0')
return bin
}
I think this would be much easier to implement than to use individual binary values of hex as mentioned in this solution.
My solution is somewhat similar to this
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Emil S. Jørgensen |
| Solution 2 | Nick Ivanov |
| Solution 3 | |
| Solution 4 | Nyi Nyi |
| Solution 5 | Alireza Rinan |
| Solution 6 | Blake Gearin |
| Solution 7 | GrayGalaxy |
