Hi, got the following code working after some effort. But have a question about the recursive code. I works fine but I cannot step through it to see what happens. Is there something that I can do to the code to see the "internal workings" at each recursion?
question1. It works but I dont get why the line with the hex+1 works correctly catching the 1st character correctly - why?
question2. Is there a way to eliminate the 2nd calling parameter, the ,int l) in function HextoDec
question3. xtod can also be substituded for #define xtod, which way is the best?
question1. It works but I dont get why the line with the hex+1 works correctly catching the 1st character correctly - why?
question2. Is there a way to eliminate the 2nd calling parameter, the ,int l) in function HextoDec
question3. xtod can also be substituded for #define xtod, which way is the best?
Code:
#define xtod(c) ((c>='0' && c<='9') ? c-'0' : ((c>='A' && c<='F') ? \
c-'A'+10 : ((c>='a' && c<='f') ? c-'a'+10 : 0)))
Code:
char xtod(char c) {
if (c>='0' && c<='9') return c-'0';
if (c>='A' && c<='F') return c-'A'+10;
if (c>='a' && c<='f') return c-'a'+10;
return c=0; // not Hex digit
}
int HextoDec(char *hex, int l)
{
if (*hex==0) return(l);
return HtoDec(hex+1, l*16+xtod(*hex)); // hex+1?
}
int xstrtoi(char *hex) // hex string to integer
{
return HextoDec(hex,0);
}
xstrtoi("fe6F2"); // works ok
xstrtoi("0xfe6F2"); //works ok
xstrtoi("0xfev6F2"); // bombs output fe06f2
Comment