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
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started