Make your own atoi() function in C
atoi() is able to translate a string of a number into an integer number. There are lots of tech to do it, and here is showing some examples.
int my_atoi(char *p) {
int k = 0;
while (*p) {
k = k*10 + (*p) - '0';
p++;
}
return k;
}
10 times can be switched to bit-wise shift since bit-wise will use smaller instruction set which enables to reduce transistor in a system. Following shows of it.
int my_atoi(char *p) {
int k = 0;
while (*p) {
k = (k<<3)+(k<<1)+(*p)-'0';
p++;
}
return k;
}
New blog post: Make your own atoi() function in C http://blog.breadncup.com/2009/12/21/make-your-own-atoi-function-in-c/