Convert char from String to int problem

I am trying to convert a char to an int.
I read online that this can be done for the Arduino by adding a 0, like this: int x = (char + 0);
However, this line of code is not working (it compiles ok - no errors. It just doesn't return a value):
int x = (str.charAt(1) + 0); // (where str = "54321")

Any help would be appreciated, thanks.

arduinobytes:
I am trying to convert a char to an int.

Am I correct to assume that in the string "54321" you want to convert only the second character ie '4'?

if that's the case, you could simple do this assuming you are using c-string and not **S**tring

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(115200);

  while (!Serial) {
    ; // wait for serial port to connect. Needed for Native USB only
  }

  char str[] = "54321";

  int x = str[1] & 0x0F; //have look at the ASCII table ;)


  Serial.println(str);
  Serial.println(x);

}

void loop() // run over and over
{

}

hope that helps....

arduinobytes:
I am trying to convert a char to an int.
I read online that this can be done for the Arduino by adding a 0, like this: int x = (char + 0);
However, this line of code is not working (it compiles ok - no errors. It just doesn't return a value):
int x = (str.charAt(1) + 0); // (where str = "54321")

Any help would be appreciated, thanks.

You seem to have that backwards, to convert an ASCII numeric character to its integer value, subtract the ASCII character for zero '0'
In your code, you are adding the number 0 to str.charAt(1), which accomplishes nothing.

sherzaad:
Am I correct to assume that in the string “54321” you want to convert only the second character ie ‘4’?

if that’s the case, you could simple do this assuming you are using c-string and not **S**tring

Unfortunately, like the title says, I am using String.
Yes, I just want to obtain a single int from the String, not the whole thing.

david_2018:
You seem to have that backwards, to convert an ASCII numeric character to its integer value, subtract the ASCII character for zero '0'
In your code, you are adding the number 0 to str.charAt(1), which accomplishes nothing.

So, am I correct in assuming that the code should be:
int x = (str.charAt(1) - '0');

pls, post your code.
(No need for wasting time on hypothetical strings or Strings and/or + - zeroes)

GRuser:
pls, post your code.
(No need for wasting time on hypothetical strings or Strings and/or + - zeroes)

String str="54321"; //will get from external source later
int x = (str.charAt(1) + 0);
Serial.print("Your number is: ");
Serial.println(x);
if (x > 3) {
  Serial.println("The number is too high!");
}
else {
  //execute code here that uses number, yet to write
}

What do you get if you try that code?

wildbill:
What do you get if you try that code?

Nothing, the Arduino “hangs”.

If you are using a standard Arduino, start over and don't use Strings. Strings cause memory problems and random program crashes.

wildbill:
What do you get if you try that code?

I have now changed the code to:

String str="54321"; //will get from external source later
int x = (str.charAt(1) - '0');
Serial.print("Your number is: ");
Serial.println(x);
if (x > 3) {
  Serial.println("The number is too high!");
}
else {
  //execute code here that uses number, yet to write
}

...as suggested earlier and it is all working fine.

Thank you for all the help.

jremington:
If you are using a standard Arduino, start over and don’t use Strings. Strings cause memory problems and random program crashes.

Thank you.
I would like to do that, but I don’t have enough programming knowledge.

arduinobytes:
...as suggested earlier and it is all working fine.

Thank you for all the help.

For now... the reason not to use Stings may present itself in a few minutes, hours, days or weeks. I just depends on how long it will take to corrupt your memory heap.

arduinobytes:
Thank you.
I would like to do that, but I don’t have enough programming knowledge.

It isn’t hard

char str []="54321";
int x = str [1] - '0';
Serial.print("Your number is: ");
Serial.println(x);
if (x > 3) {
  Serial.println("The number is too high!");
}
else {
  //execute code here that uses number, yet to write
}

See the similarity?
Enjoy the savings.

So not a string at all, just a char array?

Where does that "c-string" come into it, which someone mentioned?

A C-string is a char array, containing the contents of the string, and a (invisible) terminating zero value.
Literally zero, not the character ‘0’.

A C-string is a character array, with a zero terminating byte.

Plenty of reference material on line, if you wish to learn more about the programming language. Google "C/C++ C-strings", for example.

arduinobytes:
Where does that "c-string" come into it, which someone mentioned?

it comes from string.h of the ANSI C standard library which is built into the Arduino AVR Core, whereas Strings come from WString.h, non-ANSI C library, also part of the Arduino Core.