atoi problem

10 Jul 2014

Hi there, I'm having a problem with atoi that I can't work out. I'm trying to parse a querystring and isolate the number then convert that to an int.

Here's the code I'm having trouble with:

void writeServerUploadFreq(string suf){
    suf.replace(0,17,"");
    suf.replace(suf.find(" HTTP/1.1"),10,"");
    pc.printf("Server update freq changed = %s\n\r",suf);
    int tmp = atoi(suf);
}

But I'm getting an "No instance of function "std::atoi" matches the argument list in "main.cpp"" error.

Any ideas what I'm doing wrong?

Cheers, Darius

11 Jul 2014

I'll answer my own question:

void writeServerUploadFreq(string suf){
    suf.replace(0,17,"");
    suf.replace(suf.find(" HTTP/1.1"),10,"");
    pc.printf("Server update freq changed = %s\n\r",suf);
    char * cstr = new char [suf.length()+1];
    std::strcpy (cstr, suf.c_str());
    int tmp = atoi(cstr);
}
21 Jul 2014

atoi(suf.c_str()); ?

02 Aug 2014

Hmm, I think I tried that first but had to do some messing around to get it to work. I'll try it and see what happens.

Woohoo. it works. Many thanks. Saves some messing around and makes the code easier to understand.