Note that there are some explanatory texts on larger screens.

plurals
  1. POC Convert String to Ints Issue
    primarykey
    data
    text
    <p>I'm trying to parse some input on an embedded system. I'm expecting something like this:</p> <pre><code>SET VARNAME=1,2,3,4,5,6,7,8,9,10\0 </code></pre> <p>When I'm converting the separate strings to ints, both <code>atoi()</code> and <code>strtol()</code> seem to be returning 0 if the string begins with 8.</p> <p>Here is my code:</p> <pre><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 &gt; sizeof(value)-1) { return; } } SetVariable(name, value, i); } </code></pre> <p>Passing it:</p> <pre><code>SET VAR=1,2,3,4,5,6,7,8,9,10\0 </code></pre> <p>gives the following in my uart debug:</p> <pre><code>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 </code></pre> <p><strong>UPDATE:</strong></p> <p>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.</p> <pre><code>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 </code></pre> <p><strong>UPDATE 2:</strong></p> <p>My UARTDEBUG section currently reads:</p> <pre><code> #ifdef UARTDEBUG snprintf(convert, 20, "Long:%ld=String:%s", value[i], pch); SendLine(convert); #endif </code></pre> <p>If I comment out the <code>snprintf()</code> line, everything works perfectly. So what's going on with that?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload