I'm trying to make a program where the user enters a hexadecimal string ("in format 3ecf, no 0x and no capitals") The code below is my attempt to copy what the user enters (address) and store its binary equivalent in binAddress.
How do I fix this?
Or is there an easier way?
char address [6];//global
char binAddress[24]; //global
scanf("%s", address); //in some other function
...
void hexToBin(){
int i = 0;
int j = 24;
int z;
while(address[i]){
char x[4]; //strcpy(char x, "0000")
switch(address[i]){
case '0': strcpy(x, "0000"); break;
case '1': strcpy(x, "0001"); break;
case '2': strcpy(x, "0010"); break;
case '3': strcpy(x, "0011"); break;
case '4': strcpy(x, "0100"); break;
case '5': strcpy(x, "0101"); break;
case '6': strcpy(x, "0110"); break;
case '7': strcpy(x, "0111"); break;
case '8': strcpy(x, "1000"); break;
case '9': strcpy(x, "1001"); break;
case 'a': strcpy(x, "1010"); break;
case 'b': strcpy(x, "1011"); break;
case 'c': strcpy(x, "1100"); break;
case 'd': strcpy(x, "1101"); break;
case 'e': strcpy(x, "1110"); break;
case 'f': strcpy(x, "1111"); break;
default: strcpy(x, "0000"); break;
}
i++;
for (z = 3; z > -1; z--){
binAddress[j] = x[z];
j--;
printf("%c\n", binAddress[j]);
}
}
}