简体   繁体   English

从String读取/解析多个INT

[英]Reading/parsing multiple INTs from a String

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. 我一直在阅读有关将字符串转换为整数,“atoi”和“strol”C标准库函数以及其他一些我似乎无法理解的事情。

What I'm initially trying to do, is to get a series of numbers from a string and put them into an int array. 我最初想要做的是从字符串中获取一系列数字并将它们放入int数组中。 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". 输出来自我的真空机器人,“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. 我已经通过COM端口连接获得了上面的输出,并且我已经将它存储在一个字符串中。 (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. 在这个例子中,我正在读取一个字符串neatoOutput ,它在我从激光扫描仪请求更新之后容纳了机器人的输出。

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. “getldsscan”是我发送机器人的命令,当我得到COM输出时它才被读回来,所以我们跳过它。 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. 我希望有一个漂亮整洁的distanceArray [360] ,其中包含从机器人报告的所有距离值。 The robot will output 360 values of distance. 机器人将输出360个距离值。

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. 因此startIndex应该为我提供数据开始位置的字符索引,但正如您在上面的示例中所看到的,每个数字的值的大小范围从单个字符到最多4个字符。 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. 您会注意到我最终不需要知道我认为我需要使用的int startIndex变量。

//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;
}

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
    }

}

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 */
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM