Question: I am writing a C program that reads in decimal and hexidecimal values and displays the ascii value. I am having trouble differientiating between the

I am writing a C program that reads in decimal and hexidecimal values and displays the ascii value. I am having trouble differientiating between the two inputs. I am using isdigit and isxdigit but my input for the hexidecimal input is in the format 0x... to show that it is a hex input. How can I adjust my code to accept these values as hex and differentiate between hex and dec? I have included my code.

#include #include #include #include #include int main() { char line[1024]; for(int i=0;i<1024;i++){

line[i]='\0'; }

int len; int val; char *p; while((p = fgets(line, sizeof(line), stdin)) != NULL) { /* read a line from standard input */ len = strlen(line)-2; for(int i = 0; i < len; i++) { p = &line[i]; if(isxdigit(line[i])) { sscanf(p, "%x", &val); printf("%c", val); } else if(isdigit(line[i])){ sscanf(p, "%d", &val); printf("%c", val);

} /*else if(isalpha(line[i])) { printf("Error "); }*/ } } printf(" "); return 0;

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!