Question

I've been reading about converting strings to integers, the "atoi" and "strol" C standard library functions, and a few other things I just can't seem to get my head around.

What I'm initially trying to do, is to get a series of numbers from a string and put them into an int array. Here is snippet of the string (there are multiple lines in the single string):

getldsscan
AngleInDegrees,DistInMM,Intensity,ErrorCodeHEX
0,0,0,8035
1,0,0,8035
2,1228,9,0
3,4560,5,0
...
230,1587,80,0
231,0,0,8035
232,1653,89,0
233,1690,105,0
234,0,0,8035
...
358,0,0,8035
359,0,0,8035
ROTATION_SPEED,4.99

The output is from my vacuum robot, a "Neato XV-21". I've gotten the output above from over a COM port connection and I've got it stored in a string currently. (As the robot can output various different things). In this example, I'm reading from a string neatoOutput which houses the output from the robot after I've requested an update from its laser scanner.

The "getldsscan" is the command I sent the robot, it's just being read back when I get the COM output so we skip over that. The next line is just helpful information about each of the values that is output, can skip over that. From then on the interesting data is output.


I'm trying to get the value of the Second number in each line of data. That number is the distance from the scanner to an obstacle. I want to have a nice tidy int distanceArray[360] which houses all the distance values that are reported back from the robot. The robot will output 360 values of distance.

I'm not fussed with error checking or reading the other values from each line of data yet, as I'll get them later once I've got my head around how to extract the current basic data I want. So far I could use something like:

int startIndex = 2 + neatoOutput.find("X",0); //Step past end of line character

So startIndex should give me the character index of where the data starts, but as you can see by the example above, the values of each number range in size from a single character up to 4 characters. So simply stepping forward through the string a set amount won't work.

What I'm thinking of doing is something like...

neatoOutput.find("\n",startIndex );

Which with a bit more code I should be able to parse one line at a time. But I'm still confused as to how I extract that second number in the line which is what I want.


If anyone is interested in about hacking/coding the robot, you can goto:-



UPDATE: Resolved

Thanks for your help everyone, here is the code I'm going to work with in the near term. You'll notice I didn't end up needing to know the int startIndex variable I thought I would need to use.

//This is to check that we got data back
signed int dataValidCheck = neatoOutput.find("AngleInDegrees",0);
if (dataValidCheck == -1)
    return;

istringstream iss(neatoOutput);
int angle, distance, intensity, errorCode;
string line;

//Read each line one by one, check to see if its a line that contains distance data
while (getline(iss,line))
{
    if (line == "getldsscan\r")
        continue;
    if (line == "AngleInDegrees,DistInMM,Intensity,ErrorCodeHEX\r")
        continue;

    sscanf(line.c_str(),"%d,%d,%d,%d",&angle,&distance,&intensity,&errorCode); //TODO: Add error checking!
    distanceArray[angle] = distance;
}
Was it helpful?

Solution

Try this (untested, so maybe minor bugs):

#include <iostream>
#include <sstream>
#include <string>
#include <cstdio>
using namespace std;

int main()
{
    string s("3,2,6,4\n2,3,4,5\n");
    istringstream iss(s);
    int a, b, c, d;
    string line;
    while (getline(iss,line))
    {
        // Method 1: Using C
        sscanf(line.c_str(),"%d,%d,%d,%d",&a,&b,&c,&d);


        // Method 2: Using C++
        std::stringstream  lineStream(line);
        char  comma;
        lineStream >> a >> comma >> b >> comma >> c >> comma >> d;


        // do whatever
    }

}

OTHER TIPS

You may parse the string yourself. It's simple.

code:

int ParseResult(const char *in, int *out)
{
    int next;
    const char *p;

    p = in;
    next = 0;

    while (1)
    {
        /* seek for next number */
        for (; !*p && !isdigit(*p); ++p);

        if (!*p)
            break;  /* we're done */

        out[next++] = atoi(p);  /* store the number */

        /* looking for next non-digit char */
        for (; !*p && isdigit(*p); ++p);
    }

    return next;  /* return num numbers we found */
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top