Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8412989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:49:09+00:00 2026-06-10T00:49:09+00:00

I’ve been reading about converting strings to integers, the atoi and strol C standard

  • 0

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:-

  • http://www.neatorobotics.com/programmers-manual/
  • http://xv11hacking.wikispaces.com/


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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T00:49:10+00:00Added an answer on June 10, 2026 at 12:49 am

    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
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am reading a book about Javascript and jQuery and using one of the
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I need a function that will clean a strings' special characters. I do NOT
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.