# hex to binary

This is a discussion on hex to binary within the C Programming forums, part of the General Programming Boards category; Hi I am looking for a way of converting a hex string into binary, or at least somehow separating the ...

1. ## hex to binary

Hi

I am looking for a way of converting a hex string into binary, or at least somehow separating the bitfields using masking, shift operators or some other method. I came up with a way of manually replacing each character with its known bits like 'a' with '1010', but just found it wasn't very good.

2. Originally Posted by nasser
I came up with a way of manually replacing each character with its known bits like 'a' with '1010', but just found it wasn't very good.
It would be good if you showed us this code and explained in what way you "found it wasn't very good".

3. By "binary" do you mean a string containing the binary values or displaying it as a binary string?

Your method of replacing characters with their binary equivalents is actually about the best way (unless you did it the hard way).

Post your code, lets see what we can do...

4. What you've described to be "not very good", I would find to be very good. Pretty darn efficient in fact I imagine.
Do you want us to find a worse method or are you going to post the code?

5. The problem here is that the character array binary gets overloaded because I am reading in multiple (unknown) number of hex numbers.
I also have the problem of not knowing how many inputs there are so I don't know how to define the array.
Anyway I can circumvent these problems?

Code:
```	while (scanf("%s\n", hex) == 1) {

for (i=0; i<8; i++) {

if (hex[i] == '0') {

strcat(binary, "0000");
printf("\nIt is a 0");

}
if (hex[i] == '1') {

strcat(binary, "0001");
printf("\nIt is a 1");
}
if (hex[i] == '2') {

strcat(binary, "0020");
printf("\nIt is a 2");
}
if (hex[i] == '3') {

strcat(binary, "0011");
printf("\nIt is a 3");
}
if (hex[i] == '4') {

strcat(binary, "0100");
printf("\nIt is a 4");
}
if (hex[i] == '5') {

strcat(binary, "0101");
printf("\nIt is a 5");
}
if (hex[i] == '6') {

strcat(binary, "0110");
printf("\nIt is a 6");
}
if (hex[i] == '7') {

strcat(binary, "0111");
printf("\nIt is a 7");
}
if (hex[i] == '8') {

strcat(binary, "1000");
printf("\nIt is a 8");
}
if (hex[i] == '9') {

strcat(binary, "1001");
printf("\nIt is a 9");
}
if (hex[i] == 'a') {

strcat(binary, "1010");
printf("\nIt is a 10");
}
if (hex[i] == 'b') {

strcat(binary, "1011");
printf("\nIt is a 11");
}
if (hex[i] == 'c') {

strcat(binary, "1100");
printf("\nIt is a 12");
}
if hex[i] == 'd') {

strcat(binary, "1101");
printf("\nIt is a 13");
}
if (hex[i] == 'e') {

strcat(binary, "1110");
printf("\nIt is a 14");
}
if (hex[i] == 'f') {

strcat(binary, "1111");
printf("\nIt is a 15");
}

} //end for

} //end while```

6. Code:
`strcat(binary, "0020");`
I don't think any of us were expecting that.

If you want a string representation, then I don't see much better (other than perhaps using a switch to make it look a little cleaner). (EDIT: Or an array, as below.)

7. Yep, you did it the hard way...
Code:
```char hex[16];
char binary[128] = {0};
char convert[][] = {"0000","0001","0010","0011","0100","0101","0110","0111",
"1000","1001","1010","1011","1100","1101","1110","1111"};
int i;

// read the hex input as a string up to 16 characters here

// convert
for (i = 0; i < strlen(hex); i++);
{
if (isdigit(hex[i])
strcat(binary,convert[hex[i]-'0']);
else
strcat(binary,convert[hex[i]-'A']);
}```
This wants fleshing out with come code to make sure the number is a valid hex number (i.e. contains only 0..9 A..F) and is all caps, but it should work.

8. ^Thanks for that.

But can you see I will have the problem where the binary string will be overloaded after a few inputs?
The binary doesn't have to be a character string btw. I want to later separate the bits because these binary digits are machine codes for assembly instructions...

9. Originally Posted by nasser
But can you see I will have the problem where the binary string will be overloaded after a few inputs?
No. (I'm assuming you initialize binary somewhere at the top of the function.)

And if you don't want it as a string, then you can just store it as a number (read it in with %x) and you're done.

10. Why won't it be overloaded? I actually tried it and it does.

You defined binary as binary[128], each hex number takes 32 bits/elements, so after a few inputs it will fill up the array.

11. Originally Posted by nasser
^Thanks for that.

But can you see I will have the problem where the binary string will be overloaded after a few inputs?
The binary doesn't have to be a character string btw. I want to later separate the bits because these binary digits are machine codes for assembly instructions...
Well... you could always do this... char *Binary = malloc(4294967280 * sizeof(char));... in which case overrunning the binary buffer would be the least of our worries...

Why won't it be overloaded? I actually tried it and it does.
You defined binary as binary[128], each hex number takes 32 bits, so after a few inputs it will fill up the array.
And you can't figure out to clear the buffer before you load the next value?

Text-Binary is useless for anything but display. Your computer won't have the first clue what to do with it... Now if you were reading the hexcode and converting it to machine-binary, using fscanf("%x"... you might actually stand a chance of turning text-hex into machine code...

EDIT: Get out your dictionary and look up "Example"....

12. Originally Posted by nasser
Why won't it be overloaded? I actually tried it and it does.

You defined binary as binary[128], each hex number takes 32 bits/elements, so after a few inputs it will fill up the array.
So my assumption that you were bothering to initialize your variable was wrong. Once you get to 32 (or 64, or whatever your size is), you should be done and the next one needs to be starting from scratch.

But if you just store it as a number, then you won't have to bother.

13. I initialised it like:
Code:
` char binary[50] = {0}`
at the start of main. Is this right?

14. For the first time you use it. But you need to initialize it every time you want to use it, not just once.