I hope someone can help me out on this, I've pretty much been driven to desperation. I've been on c for only a few weeks, and this is the only bump I've come across, and it's driving me nuts. I've been using various webpages as guides, mainly...
I'm trying to write a simple prog to convert a Hexadecimal number to a binary string. I used stdio and string libraries, and declared 2 char arrays (a & c). From what I know about Hexa -> binary conversion, each Hexa digit represents four binary digits. 8 -> 1000, 2 -> 0010, etc.
So I basically did this:
I have 15 more of those if blocks, one for each other Hexa digit. Yeah, I know its kinda awkward and primitive...but its the best I can do. The answer DOES come out...the binary bit...but its followed by a string of garbage, and I have no idea how to get rid of it.Code:#include <stdio.h> #include <string.h> int main (void) { char a[100]; int b; char c[100]; int d = 0; int e = 1; int f = 2; int h = 3; scanf ("%s", a); for (b = 0; b < strlen(a); b = b + 1){ if (a[b] == '0'){ c[d] = '0'; c[e] = '0'; c[f] = '0'; c[h] = '0'; d = d + 4; e = e + 4; f = f + 4; h = h + 4; } } printf ("%s", c); return 0; }
Can anyone help? Please? I can't figure out what's wrong... Thanks.
...oh, and can anyone make suggestions on good c books to read?