Delta_G:
It doesn't. You weren't reading where you thought you were. Imagine that a started out with a 1 in it. You'd never see the 8 and you'd parse the two 0's and get 0 as an answer. You weren't reading in the "800". You were probably reading somewhere off in who knows what part of memory.
To prove it, go back to your old code and print a so you can see what value it was getting. Now go and use that value to initialize a and see that you get the exact same wrong result. What if a was getting the initial value of 64? What character was in inbuf[64]? That's the character being parsed, not your "800"
Debug results with 'a' initialized:
htoli("64")=
c(chr)=6 b=0 after mul b=0 c(dec)=6, after add (b=b+c) b=6
c(chr)=4 b=6 after mul b=96 c(dec)=4, after add (b=b+c) b=100
result =100
returned value of htoli("64")=100
htoli("64")=
c(chr)=6 b=0 after mul b=0 c(dec)=6, after add (b=b+c) b=6
c(chr)=4 b=6 after mul b=96 c(dec)=4, after add (b=b+c) b=100
result =100
returned value of htoli("buf")=100
the calling code:
char buf[10];
uint32_t bigValue = htoli("64");
Serial.print("returned value of htoli(\"64\")=");
Serial.println(bigValue,DEC);
sprintf(buf,"64");
bigValue = htoli(buf);
Serial.print("returned value of htoli(\"buf\")=");
Serial.println(bigValue,DEC);
Debug output with the initialization code for 'a' removed:
htoli("64")=
c(chr)=6 b=0 after mul b=0 c(dec)=6, after add (b=b+c) b=6
c(chr)=4 b=0 after mul b=0 c(dec)=4, after add (b=b+c) b=4
result =0
returned value of htoli("64")=0
htoli("64")=
c(chr)=6 b=0 after mul b=0 c(dec)=6, after add (b=b+c) b=6
c(chr)=4 b=0 after mul b=0 c(dec)=4, after add (b=b+c) b=4
result =0
returned value of htoli("buf")=0
The code that produces this error:
uint32_t htoli(const char inbuf[]){
uint32_t b=0;
uint16_t a;
uint8_t c;
Serial.print("htoli(\"");
Serial.print(inbuf);
Serial.print("\")=\n");
bool err=false;
while((c=inbuf[a++])&&!err){
c = toupper(c);
Serial.print(" c(chr)=");
Serial.print((char)c);
if(((c>='0')&&(c<='9'))||((c>='A')&&(c<='F'))){
Serial.print(" b=");
Serial.print(b,DEC);
Serial.print(" ");
b=b*16;
Serial.print(" after mul b=");
Serial.print(b,DEC);
Serial.print(" c(dec)=");
c= c-48;
if(c>9)c=c-7;
b = b + c;
Serial.print(c,DEC);
Serial.print(", after add (b=b+c) b=");
Serial.print(b,DEC);
Serial.print("\n");
}
else {
Serial.print("error? ");
err=true;
}
}
Serial.print("result =");
Serial.println(b,DEC);
return b;
}
This is a reproducible error. Something is wrong.
Chuck.