Pages: [1]   Go Down
Author Topic: Read from one character in string to another  (Read 398 times)
0 Members and 1 Guest are viewing this topic.
Minnesota!
Offline Offline
Sr. Member
****
Karma: 0
Posts: 388
My nickname on the forum used to be Soapy29.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello everyone,

I have a string of data in one of my Arduino programs, and I need to break it apart. The string will hold things like:
M1:1023;
M2:25;

or other things like that. I am wondering how I can great only things that are between : and ; so for M1, I would get 1023 saved to a variable. For M2, 25 would be saved to that variable.

----

I was watching a video on Youtube about I2C, and Jeremy did, what he used things like data_length(); and data.substring(); except that was in Processing.
Logged

Andrew K.

France
Offline Offline
Edison Member
*
Karma: 38
Posts: 1012
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello smiley

Using sscanf is the easiest solution I think.

Code:
int var;
char str[] = "M1:1023;";
sscanf( str, "M1:%d;", &var );
// var is now = 1023

Edit: sorry I meant, sscanf not sprintf smiley-wink
« Last Edit: March 15, 2013, 07:33:47 pm by guix » Logged

UK
Offline Offline
Faraday Member
**
Karma: 100
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello smiley

Using sprintf is the easiest solution I think.

Code:
int var;
chat str[] = "M1:1023;";
sprintf( str, "M1:%d;", &var );
// var is now = 1023
Not with sprintf you won't.  That will put "M1:308" into the variable str (where 308 is the address of the variable "var").

I think you meant sscanf().
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

France
Offline Offline
Edison Member
*
Karma: 38
Posts: 1012
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Of course... I'm a little tired smiley-zipper

There is another solution that is, using atoi, like this:

Code:
char str[] = "M1:1023;";
int var = atoi( &str[3] );
//var now = 1023
Logged

Minnesota!
Offline Offline
Sr. Member
****
Karma: 0
Posts: 388
My nickname on the forum used to be Soapy29.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Awesome! Thank you. I don't know anything about atoi, but wouldn't that make var = 1023; instead of 1023?
Logged

Andrew K.

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 653
Posts: 50890
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but wouldn't that make var = 1023; instead of 1023?
int var = 27;
What is the value in var? 27 or 27;?

If you said 27;, you were wrong.
Logged

Minnesota!
Offline Offline
Sr. Member
****
Karma: 0
Posts: 388
My nickname on the forum used to be Soapy29.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but wouldn't that make var = 1023; instead of 1023?
int var = 27;
What is the value in var? 27 or 27;?

If you said 27;, you were wrong.

Ok, I was just curious because I have never used atoi before.
Logged

Andrew K.

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 653
Posts: 50890
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
because I have never used atoi before.
   
Or googled it either, apparently.
Logged

Pages: [1]   Go Up
Jump to: