Convert Hex to Binary

convert hex to binary in javascript

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

Convert hexadecimal to binary

Easily. Each digit in a hex number translates to 4 digits in a binary number, so you only need to know the binary numbers from 0 to f, or 0000 to 1111.

As an example:

0xc3e2

c = 12 decimal = 1100
3 = 0011
e = 14 decimal = 1110
2 = 0010

Then just string them together.

0xc3e2 = 1100001111100010 binary

Converting from hex to binary without losing leading 0's python

I don't think there is a way to keep those leading zeros by default.

Each hex digit translates to 4 binary digits, so the length of the new string should be exactly 4 times the size of the original.

h_size = len(h) * 4

Then, you can use .zfill to fill in zeros to the size you want:

h = ( bin(int(h, 16))[2:] ).zfill(h_size)

How to convert Hex to binary in C and output binary as a char array?

There are 2 major problems in your code:

  • char output[] = ""; defines an array of char just large enough to hold the empty string. For you purpose, you should allocate an array with a size 4 * strlen(input) + 1
  • return output; returns the address of a local object, which will become invalid as soon as the function returns. You could instead allocate memory from the heap with malloc() and return the pointer. The caller will be responsible for freeing this object.

Here is a modified version:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *hex_to_bin(const char input[]) {
char *output = malloc(strlen(input) * 4 + 1);
if (output != NULL) {
char *p = output;
*p = '\0';
for (size_t i = 0; input[i]; i++) {
switch (input[i]) {
case '0':
strcpy(p, "0000");
break;
case '1':
strcpy(p, "0001");
break;
case '2':
strcpy(p, "0010");
break;
case '3':
strcpy(p, "0011");
break;
case '4':
strcpy(p, "0100");
break;
case '5':
strcpy(p, "0101");
break;
case '6':
strcpy(p, "0110");
break;
case '7':
strcpy(p, "0111");
break;
case '8':
strcpy(p, "1000");
break;
case '9':
strcpy(p, "1001");
break;
case 'A':
case 'a':
strcpy(p, "1010");
break;
case 'B':
case 'b':
strcpy(p, "1011");
break;
case 'C':
case 'c':
strcpy(p, "1100");
break;
case 'D':
case 'd':
strcpy(p, "1101");
break;
case 'E':
case 'e':
strcpy(p, "1110");
break;
case 'F':
case 'f':
strcpy(p, "1111");
break;
default:
p[0] = input[i];
p[1] = '\0';
break;
}
p += strlen(p);
}
}
return output;
}

int main() {
char *p = hex_to_bin("n = DeadBeef\n");
printf("%s\n", p);
free(p);
return 0;
}

Converting from hexadecimal to binary values

The output you get is correct, it just needs to be formatted a bit. The leading 0b indicates that it is a binary number, similarly to how 0x stands for hexadecimal and 0 for octal.

First, slice away the 0b with [2:] and use zfill to add leading zeros:

>>> value = '05808080'
>>> b = bin(int(value, 16))
>>> b[2:]
'101100000001000000010000000'
>>> b[2:].zfill(32)
'00000101100000001000000010000000'

Finally, split the string in intervals of four characters and join those with spaces:

>>> s = b[2:].zfill(32)
>>> ' '.join(s[i:i+4] for i in range(0, 32, 4))
'0000 0101 1000 0000 1000 0000 1000 0000'

If you can live without those separator spaces, you can also use a format string:

>>> '{:032b}'.format(int(value, 16))
'00000101100000001000000010000000'


Related Topics



Leave a reply



Submit