how to store last 3 values of a string to an int type variable
String a = Serial.readString(); //let input be aaa xyz 123
int b = last 3 char of string a // b has to store last 3 charecters as intiger value
how to store last 3 values of a string to an int type variable
String a = Serial.readString(); //let input be aaa xyz 123
int b = last 3 char of string a // b has to store last 3 charecters as intiger value
You ask for string, you code uses String. They are not the same, which is it? (I hope the first...)
The String class has two possibly interesting methods, substring() and toInt(). You can research how to use them, if you insist on pissing away resources using the String class. There ARE better ways.
The following codes could be helpful for you; however, you follow the remarks of Post#2 and try to write codes avoiding the use of String class. In this example, you enter the known string aaa xyz 123 from the InputBox of the Serial Monitor. The integer value 123 will be echoed back in the Serial Monitor.
String a="";
String myData = "";
void setup()
{
Serial.begin(9600);
}
void loop()
{
//int b = last 3 char of string a
// b has to store last 3 charecters as intiger value
}
void serialEvent()
{
while (Serial.available() > 0)
{
a += Serial.readString(); //let input be aaa xyz 123
Serial.print(a);
}
for(int i = 8, j=0; i<11, j<3; i++, j++) //accumulating those members which contain integer numbers
{
myData += a[i];
}
long x = myData.toInt(); //converting only the elements that contain integer numbers
Serial.println(x, DEC); //shows: 123
Serial.print(x, HEX); // shows: 7B which is hex equivalent of 123
}
If you insist on using Strings then you might just as well go the whole hog and use a raft of String functions to mess up the memory as much as possible
String theString = "Hello World 123";
int theInt = theString.substring(theString.length() - 3, theString.length()).toInt();
There ARE better ways.
Who is going to cite examples using these better ways?
I'd start by thinking about using strlen, and subtracting from that.
GolamMostafa:
Who is going to cite examples using these better ways?
Should meet the requirements. Complies. Untested...
char buffer[4];
int index;
unsigned long mark;
int your_int_is_here;
void setup( void )
{
}
void loop( void )
{
while ( Serial.available() )
{
buffer[index] = Serial.read();
if ( index >= 3 )
{
buffer[0] = buffer[1];
buffer[1] = buffer[2];
buffer[2] = buffer[3];
buffer[3] = 0;
}
else
++index;
}
if ( index == 0 )
{
mark = millis();
}
if ( millis() - mark >= 1000 )
{
your_int_is_here = atoi( buffer );
index = 0;
memset( buffer, 0, sizeof(buffer) );
}
}
@GolaMostafa
In for loop 2 conditions shouldnt be checked via && or ||? With comma only second condition will be evaluated i.e. j<3.
GolamMostafa:
Who is going to cite examples using these better ways?
char thestring[] = {"Hello World 123"};
void setup()
{
Serial.begin(115200);
int theInt = 0;
for (int c = strlen(thestring) - 3; c < strlen(thestring); c ++)
{
theInt = (10 * theInt) + (thestring[c] - '0');
}
Serial.println(theInt);
}
void loop()
{
}
char thestring[] = {"Hello World 123"};
void setup()
{
Serial.begin(115200);
int theInt = 0;
for (int c = max (0, strlen(thestring) - 3); c < strlen(thestring); c ++)
{
theInt = (10 * theInt) + (thestring[c] - '0');
}
Serial.println(theInt);
}
void loop()
{
}
abhijithasdeveloper:
how to store last 3 values of a string to an int type variable
Have a look at the parse example in Serial Input Basics
It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.
...R
TolpuddleSartre:
char thestring[] = {"Hello World 123"};
void setup()
{
Serial.begin(115200);
int theInt = 0;
for (int c = max (0, strlen(thestring) - 3); c < strlen(thestring); c ++)
{
theInt = (10 * theInt) + (thestring[c] - '0');
}
Serial.println(theInt);
}
void loop()
{
}
I couldn't have put it better myself
thanks everyone.