Search code examples
cstringatoi

C Convert String to Ints Issue


I'm trying to parse some input on an embedded system. I'm expecting something like this:

SET VARNAME=1,2,3,4,5,6,7,8,9,10\0

When I'm converting the separate strings to ints, both atoi() and strtol() seem to be returning 0 if the string begins with 8.

Here is my code:

char *pch, *name, *vars;
signed long value[256];
int i;

#ifdef UARTDEBUG
    char convert[100];
#endif
if(strncmp(inBuffer, "SET",3)==0)
{
    pch = strtok(inBuffer," ");
    pch = strtok(NULL," ");
    name = strtok(pch, "=");
    vars = strtok(NULL,"=");

    pch = strtok(vars,",");

    i = 0;
    while(pch != NULL)
    {
        value[i] = atoi(pch);
        #ifdef UARTDEBUG
            snprintf(convert, sizeof(convert), "Long:%d=String:\0", value[i]);
            strncat(convert, pch, 10);
            SendLine(convert);
        #endif
        i++;
        pch = strtok(NULL,",");

        // Check for overflow
        if(i > sizeof(value)-1)
        {
            return;
        }
    }    

    SetVariable(name, value, i);
}

Passing it:

SET VAR=1,2,3,4,5,6,7,8,9,10\0

gives the following in my uart debug:

Long:1=String:1                                                                
Long:2=String:2                                                                
Long:3=String:3                                                                
Long:4=String:4                                                                
Long:5=String:5                                                                
Long:6=String:6                                                                
Long:7=String:7                                                                
Long:0=String:8                                                                
Long:9=String:9                                                                
Long:10=String:10

UPDATE:

I've checked the inBuffer both before and after 'value[i] = atoi(pch);' and it's identical and appears to have been split up to the right point.

S  E  T     V  A  R     1     2     3     4     5     6     7     8     9  ,  1  0
53 45 54 00 56 41 52 00 31 00 32 00 33 00 34 00 35 00 36 00 37 00 38 00 39 2c 31 30 00 00 00 00 

UPDATE 2:

My UARTDEBUG section currently reads:

        #ifdef UARTDEBUG
            snprintf(convert, 20, "Long:%ld=String:%s", value[i], pch);
            SendLine(convert);
        #endif

If I comment out the snprintf() line, everything works perfectly. So what's going on with that?


Solution

  • can't you try to write your own atoi? it's like ten lines long and then you can debug it easily (and check where the problem really is)

    • '0' = 0x30
    • '1' = 0x31

    and so on, you just need to do something like

    string[x] - 0x30 * pow(10, n)
    

    for each digit you have