Breaking a string

Hi,

I learning about C++ on the run. I am a retired Pascal programmer and last used C when K&R got to about revision 0.0.3. :) OK, well maybe not that far back but a looong ways back. I am building a home auto system with Arduinos (too many jerks on the Arduino forums - so here I am)

I am sending and receiving data in the following 50-char-wide String
String myData = "wwxxyyzz:data-in-here-then--right-padded to 50..."

ww = 10 to 99 as the station ID sending the string
xx = 10 to 99 as the station ID to receive the the string
yy = 10 to 99 as the type of Sensor sending the string
zz = 10 to 99 as the number of devices on that Sensor sending the string
I didn't want to mess with numbers below 10 as they may be
interpreted as octal, not that it really matters but 89 of anything
is more than enough.
psesudo code...
idSend = copy bytes 1&2 then convert to int to use in switch(idSend)
idRecv = copy bytes 3&4 then convert to int to use in switch(idRecv)
idSens = copy bytes 5&6 then convert to int to use in switch(idSens)
idCntr = copy bytes 7&8 then convert to int to use in switch(idCntr)

1
2
3
4
5
6
7
8
9
  switch(idSend)
  { case 10:
      switch(idSens)
      { case 10:
        // do something
        break;
      }
    break;
  }


This is really simple in Pascal, but I am struggling with it in Arduino's C++
I have looked at charAt etc but not making much headway.

Some help here would be appreciated.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <string>
#include <iomanip>
#include <regex>

int main()
{
    const std::string my_data = "12345678:data-in-here-then--right-padded to 50..." ;

    {
        try
        {
            // simle parsing with string::substr()

            // http://en.cppreference.com/w/cpp/string/basic_string/stol
            // http://en.cppreference.com/w/cpp/string/basic_string/substr
            const int sender = std::stoi( my_data.substr(0,2) ) ;
            const int receiver = std::stoi( my_data.substr(2,2) ) ;
            const int sensor = std::stoi( my_data.substr(4,2) ) ;
            const int n_devices = std::stoi( my_data.substr(6,2) ) ;
            const std::string data_in = my_data.substr(9) ;

            std::cout << "sender: " << sender << "  receiver: " << receiver
                      << "  sensor: " << sensor << "  #devices: " << n_devices << '\n'
                      << "data-in: " << std::quoted(data_in) << "\n\n" ;
        }
        catch( const std::exception& )
        {
            std::cerr << "parse error\n" ;
        }
    }

    {
        // regular expressions: very useful for more complex parsing
        
        // \d - decimal digit   () - numbered capture group   .+ - sequence of one or more characters
        std::regex regex( "(\\d\\d)(\\d\\d)(\\d\\d)(\\d\\d):(.+)" ) ;
        std::smatch match ;

        // http://en.cppreference.com/w/cpp/regex/regex_match
        if( std::regex_match( my_data, match, regex ) )
        {
            const int sender = std::stoi( match[1] ) ;
            const int receiver = std::stoi( match[2] ) ;
            const int sensor = std::stoi( match[3] ) ;
            const int n_devices = std::stoi( match[4] ) ;
            const std::string data_in = match[5] ;

            std::cout << "sender: " << sender << "  receiver: " << receiver
                      << "  sensor: " << sensor << "  #devices: " << n_devices << '\n'
                      << "data-in: " << std::quoted(data_in) << '\n' ;
        }
        else
        {
            std::cerr << "parse error\n" ;
        }
    }
}

http://coliru.stacked-crooked.com/a/e69f8644902e7550
Awesome JLB, thanks very much.

Part of my problem is trying to find out what search terms to use. I sat through some youtube tutorials but they tend to only cover the basic method of programming, albeit with a new language. I am OK with all of that but the "stoi"and "substr" are definitely what I needed, but didn't know what to look for.

Thanks again. much appreciated.
Topic archived. No new replies allowed.