How to convert string "567" into integer without using atoi ?

How to convert string "567" into integer without using atoi ? Please, give a complete example code if possible. Thanks

‘7’ - ‘0’ = 7

‘6’ - ‘0’ = 6
6 * 10 = 60

‘5’ - ‘0’ = 5.
5 * 100 = 500

And so on.

int i = (int) strtol("567", NULL, 10);
int i = 567;

My question is simple: I have a parsed string that has the following range: "000" to "999" (consider that only these values will be converted). I need to convert it to any feasible variant of integer datatype.

atoi is my last option because I do not want to evaluate array of characters from a string everytime I need such conversion. Usually I use Serial functions to solve such problem. But, at this current project, I am not dealing with Serial ports.

Thanks.

What is wrong with atoi? Anything you do will need to work in a similar way to that.

ionito:
… I do not want to evaluate array of characters from a string everytime I need such conversion. Usually I use Serial functions to solve such problem. But, at this current project, I am not dealing with Serial ports.

You’ve completely lost me here. Serial ports are nothing to do with converting strings into integers. And in any case the method suggested by AWOL would do it, if you happened to just want to convert 3 characters into an int.

Look, I Googled an example of atoi:

int atoi(char s[])
{
int i, n=0;
for(i=0; s[i]>='0' && s[i]<='9'; i++)
  n = 10*n + (s[i] - '0');
return n;
}

That stops when it runs out of digits. What’s wrong with that? And that would be what atoi does anyway. So you point it to your 3 digits and it converts them for you.

ionito: I do not want to evaluate array of characters from a string

In this case you should reconsider the subject title of the thread, since that is exactly what it says you do want to do.

Here is what I am using when I send a string like "567" by Serial to Arduino and I want it as integer:

int converted_string= 0;
while (Serial.available() > 0) converted_string = converted_string*10+(Serial.read()-'0');
// character == 567 here

anio:
while (Serial.available() > 0) converted_string = converted_string*10+(Serial.read()-‘0’);

That code relies on the number coming in a “burst”. If you got “56” “7” then it won’t work.

It works for me. How long the pause should be to crash it? In the sketch that I am using it always comes in a "burst".

In the sketch that I am using it always comes in a “burst”.

It probably comes in a burst, not because of the sketch, but because you’re using the serial monitor, I’m guessing.

It won’t work with a terminal emulator.

Yes in my case it comes from a GSM by serial and I am expecting it.