Notifications
Clear all

Parsing strings

7 Posts
5 Users
0 Likes
1,746 Views
(@queenidog)
Member
Joined: 5 years ago
Posts: 17
Topic starter  

The article on the 433 MHz radio was great.  In the program a string containing humidity and temperature information is transmitted to the receiver, where it is put back together and result printed.  I understand (roughly) how it is done for TWO items but how would one code for THREE or more items.  I tried using the StringSplitter library but that was even more confusing since the example was all in one program file.  I'm using Arduinos UNOs (and C++).  Anyone?


   
Quote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2042
 

I don't know what article on the 433 MHz radio you are referring to?
If I could see the relevant code example it would help.
Splitting strings isn't hard and I wouldn't bother with a library myself.
Looking at the StringSplitter example it looked simple enough to use.

 


   
ReplyQuote
 mday
(@mday)
Member
Joined: 5 years ago
Posts: 9
 
 
Here is an untested snippet:
 
String str1;
String str2;
String str3;

// The entire message is in the String str_out
// Split string into three values

// First, find the offsets of the two commas
int comma1 = str_out.indexOf(",");
int comma2 = str_out.index(",", comma1+1);

// Then extract the substrings
str1 = str_out.substring(0, comma1); // before first comma
str2 = str_out.substring(comma1+1, comma2); // between the two commas
str3 = str_out.substring(comma2+1); // after the second comma
Hope that helps,
-Mark

 


   
ReplyQuote
dxj
 dxj
(@dxj)
Member
Joined: 4 years ago
Posts: 27
 

Something else to consider:

  int day, year;
char weekday[20], month[20], data[100];

strcpy(data,"Friday,February,14,2019");
sscanf(data, "%[^,],%[^,],%d,%d", weekday, month, &day, &year );
Spoiler
About [^],
The parser %[^], instructs the sscanf function to read characters other than comma until you get to a comma. This is not needed if the data is delimited by white-space - i.e. "%s %s %d %d" works otherwise.

 

This post was modified 4 years ago 4 times by dxj

   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@mday

It looks fine, except for the line, which should be using the same function:

    int comma2 = str_out.indexOf(",", comma1+1);

If that's all you want to do then this is fine, and reasonably safe.

Cheers!


   
ReplyQuote
(@queenidog)
Member
Joined: 5 years ago
Posts: 17
Topic starter  

@mday

Thanks Mark.   That's what I wanted.  I was close.  Your code looks simpler than original offering.   Haven't tried it yet, but it gives me ideas...


   
ReplyQuote
(@queenidog)
Member
Joined: 5 years ago
Posts: 17
Topic starter  

@mday

Mark, your code works.   At first my last entry (volts) ended with a bunch(5 or 6) of odd characters, so I changed this line: uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];   to this line: uint8_t buf[16];, guessing that the buffer size was (3x5)+ 1 for null character.   This cleaned it up.  My question is why the first statement didn't work (taken from RadioHead library)?
Complete code here:


This post was modified 4 years ago by queenidog

   
ReplyQuote