Question
#include #include #include int main() { char line[133]; int len; int val; char *p; if( (p = fgets(line, 132, stdin)) != NULL) { /* read
#include #include #include
int main() { char line[133]; int len; int val; char *p;
if( (p = fgets(line, 132, stdin)) != NULL) { /* read a line from standard input */ len = strlen(line); printf("I got len=%d:***%s*** ", len, line); /* print the line */ strcpy(line, " 65 0x36"); /* give line a new value */ //p = &line[3]; }
for(int i = 0; i < len; i++) { p = &line[i]; sscanf(p, "%d", &val); printf("val=%d,***%c*** ", val, val); p = &line[i]; sscanf(p, "%x", &val); printf("val=%x,***%c*** ", val, val); } }
I am trying to convert input that is either decimal or hexidecimal and convert it into the asci value. I have included my code for clarification. There is no need for loops to convert to decimal or to string, rather I need to use sscanf to change the string to the integer and printf to convert that value to an ascii character. I am trying to figure out what string to put in the sscanf function call and what format and also how to seperate the lines into a list of tokens. What is the simplest way to go about accomplishing this task? My current output is just alternating between val=65,***A*** val=65,***e***
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