I'm trying to write a function that converts hex char into binary array so it's easy to read all the ones and zeros one by one. Am having problems with the code. I currently have written the code below. It gives me an error saying that it returns an address of a local variable, even though I tried to make it return a char.
char *hex_to_bin(const char input[]) {
char output[] = "";
int i = 0;
while (input[i]) {
switch (input[i]) {
case '0':
strcat(output, "0000");
break;
case '1':
strcat(output, "0001");
break;
case '2':
strcat(output, "0010");
break;
case '3':
strcat(output, "0011");
break;
case '4':
strcat(output, "0100");
break;
case '5':
strcat(output, "0101");
break;
case '6':
strcat(output, "0110");
break;
case '7':
strcat(output, "0111");
break;
case '8':
strcat(output, "1000");
break;
case '9':
strcat(output, "1001");
break;
case 'A':
strcat(output, "1010");
break;
case 'B':
strcat(output, "1011");
break;
case 'C':
strcat(output, "1100");
break;
case 'D':
strcat(output, "1101");
break;
case 'E':
strcat(output, "1110");
break;
case 'F':
strcat(output, "1111");
break;
case 'a':
strcat(output, "1010");
break;
case 'b':
strcat(output, "1011");
break;
case 'c':
strcat(output, "1100");
break;
case 'd':
strcat(output, "1101");
break;
case 'e':
strcat(output, "1110");
break;
case 'f':
strcat(output, "1111");
break;
}
i++;
}
return output;
}
Perhaps this is a silly approach, but I only recently picked up C.
char output[] = "";That array can only hold one char. You need to pick a size. Exchar output[128] = "";return output;is going to blow up too.outputis a local array.