[solved] simple programming question .

this function :

unsigned short str_num (char str[]){
unsigned char i=0; 
unsigned char len=strlen(str);
unsigned short output=0;
while(str[i]) {output+=(str[i]-'0')*pow(10,len-i-1); i++;}
          return output;

str_num("5") returns 5 str_num("55") returns 5 str_num("555") returns 554? str_num("1247") returns 1246?

why is the last number missing one for numbers superior than 99?

It is caused by rounding of when converting from double (returned by pow) to int.


while(str[i]!=0) {output= output*10 +str[i]-'0'; i++;}

and you can get rid of the strlen() call.


thank you Holmes