I need to 'extract' an integer value from a String object. This is my current code:
String myStringValue = "received: 25";
int myIntegerValue = int(myStringValue.substring(10));
It separates the string "25" without any problems, but errors come when I try to convert it to an int. Any suggestions on getting this, or any alternative method, to work? Thanks!
It's actually two issues: converting a string to an integer, and converting a String object into a char array.
Here's one way to do that:
String myStringValue = "received: 25";
String numberValue = myStringValue.substring(10);
// now convert
char buff[16]=""; // should be big enough for most numbers!
numberValue.toCharArray(buff,sizeof(buff));
int value=atoi(buff);
toInt uses the C atol function to return numeric representation of a String as a long integer.
this function discards whitespace characters (spaces and tabs) until the first non-whitespace character is found. The following sequence of digits (with an optional initial plus or minus sign) are interpreted as a numeric value and returned.
Good question, think that "the code is the documentation" is true in Arduinoland. That statement is related to "In case of discepancies between documentation, comments and code, the code is allways right".
in the end the quality of the Arduino documentation is as good as the volunteers make it..
I didn't say the documentation is bad, on contrary, in general it is good (enough for me). But at some points it is incomplete and you have to dive into the code.