Converting a string to int in arduino?

Hello, sorry if this is a bit of a noob question, relatively new to c++ and the arduino language.

Basically, I'm trying to convert the string amount from the array infoArray into an int in order to run the for loop below. (if I don't convert to int, it tells me that i isn't defined. I think that's the issue?) I know that infoArray[2] is always a one digit number as a string.

I've tried the function toInt() but it gives me the error below:

request for member 'toInt' in 'amount', which is of non-class type 'int'

Thank you for your help

void dispenser(){
  amount = infoArray[2];
  for (i=amount; i>0; i--){
  Serial.println("very good");

Wen you post code can you please post all of it. Most of the time when you only post a snippet the problem is elsewhere.


amount = atoi(infoArray);


If always absolutely that true,

amount = infoArray[0] - '0';

You can’t have a one digit number as a string. You need at least two bytes for a string.
Is it an ASCII char?
If so turn it into a number by
number = digit & 0xF;

Hi. Thanks for your help. Just to clarify, infoArray would looks something like {'g', '2', '3', ' '}, so converting the entire array through atoi wouldn't work. (I think that's what snippet #1 meant?)

Also, can you please explain what the second snippet (amount = infoArray[0] - '0') is doing?
Thank you

Hi. So regarding the entire code, I'm on vacation and don't have it at the moment, but everything else worked fine, I've only started running into issues with this function. A basic run through of the program would be the arduino receives a string through serial, and then takes each character and puts them into an array. Then it would perform certain actions based on what numbers/characters are where.

For your second post, yes, I realized that the array wasn't a string and an ascii char instead, but when I tried yours and chrisnightley's solutions, the program returned that i in the for loop wasn't defined, which tells me that amount wasn't actually converted to an int. (I think?)

Thanks for your help

Forget it, if that condition.

Subtract '0'(character) from '0'(character) to get 0 (number).
Subtract '0'(character) from '1'(character) to get 1 (number).
Subtract '0'(character) from '2'(character) to get 2 (number).
Subtract '0'(character) from '3'(character) to get 3 (number).
... to 9

See ASCII code for more realization.

This will work.

char infoArray[] = {'g', '2', '5', '9'};
int amount1 = infoArray[1]-'0'; // assign numerical 2
int amount2 = infoArray[2]-'0'; // assign numerical 5
int amount3 = infoArray[3]-'0'; // assign numerical 9

Hi. That was a good explanation and the code did the trick, thank you!

My second solution was exactly the same as @chrisknightley
So what ever was wrong it is not what I told you to do. It was probably that you had not implemented it correctly, I just gave you and example using variables that reflected what you needed to do.
If you had not declared ’amount’ as a variable previously in your code and used i as the loop variable then yes you will get an error saying the variable i was not declared.

& 0xF Has exactly the same effect as - '0'

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.