atoi() is able to translate a string of a number into an integer number. There are lots of tech to do, 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; }
thanks for help
can you please explain this string ?:
=> k = k*10 + (*p) – ‘0’;
k holds an integer, and it is recalculated by looking at the each character from the beginning given p. (*p) tells you the character’s ASCII code and ‘0’ as well. The difference gives the actual integer number. k*10 is shifting of previous value.
For example, if p is given by “425”,
The first calculation of k = 0*10 + (*p) – ‘0’ = 0 + 52-48 = 4
The next calculation of k = 4*10 + (*p) – ‘0’ = 40 + 50 – 48 = 42
The final calculation of k = 42*10 + (*p) – ‘0’ = 420 + 53-48 = 425
Character ASCII code of “4” is 52, “2” is 50, “5” is 53, and “0” is 48.
Hope this would help you,
Daniel
Thank u !!
thanks for your help mate, thats help me 😀
Hi Daniel,
I have this doubt about the code you posted, you said in one of the comments that k has an ASCII equivalent. Does the atoi version you wrote changes its behaviour if we use other encoding rather than ASCII?, say UTF-8.
Great blog.
—
Carlos
Thanks, Carlos.
You’re right. It doesn’t support UTF-8, and the code I made was only for simple implementation showing how atoi() can be implemented, and it would be useful for job interview or some sort of things. It’s not fully for a product readiness or something like that.
Thank you for your interest and pointing out of it.
Daniel
Nice. For those of you who can’t figure why k << 3 + k << 1 is used instead.
k * 10 = k * 8 + k * 2 = k * 2^3 + k * 2 ^ 1 = k << 3 + k << 1
So k * 10 + *p – '0' is nothing but k<<3 + k<<1 + *p – '0'.
i have doubt that whether atoi()function can take string charcters i.e(abcd )or not
No. The simple function doesn’t check the general error cases.
As I’ve mentioned, this atoi() is just showing the simple way to do it, and want to show the “shift” is a little better way in performance in Small Embedded System.
what about negative numbers ??
It will not handle negative numbers 🙂
in the first post inside while loop add following conditions. then it works like atoi for negative and invalid inputs
if(*p == ‘-‘ && i == 0)
{
isNegative = TRUE;
p++;
continue;
}
if(*p ‘9’)
break;
I’m not sure if your code works, but thanks for pointing of negative numbers.
“(k<<3)+(k<<1)" does not mean k*10, it means k*16(k<<4). so I think your codes have some problems.
No, you are confusing the associative property of addition and associative property of multiplication.
k*2^3+k*2=k*(2^3+2). It cannot be k*(2^4) because the power cannot be combined with addition. If it is multiplication, then you can combine it.
So, the code of my logic is correct,
Thanks,
Daniel
this is my solution: http://blog.csdn.net/njnu_mjn/article/details/9099405
hope help.
Thank you for sharing your code. Your code would be generic converting string to number.
Thanks,
Daniel
For more detail you can see this link: http://www.aticleworld.com/2016/03/how-to-use-atoi-and-how-to-make-own-atoi.html