C 将字符串转换为整数问题

发布于 2024-07-30 06:13:11 字数 2434 浏览 6 评论 0原文

我正在尝试解析嵌入式系统上的一些输入。 我期待这样的结果:

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

当我将单独的字符串转换为整数时,如果字符串开始,atoi()strtol() 似乎都会返回 0 8.

这是我的代码:

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

传递它:

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

在我的uart调试中给出以下内容:

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

更新:

我在'value[i] = atoi(pch);'之前和之后检查了inBuffer 它是相同的,并且似乎已被分割到正确的位置。

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 

更新2:

我的UARTDEBUG部分当前显示:

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

如果我注释掉snprintf()行,一切都会完美运行。 那么这是怎么回事呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

伪心 2024-08-06 06:13:11

你不能尝试编写自己的atoi吗?
就像十行那么长,然后你可以轻松调试它(并检查问题到底在哪里)

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

等等,你只需要

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

对你拥有的每个数字做类似的事情

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

娇妻 2024-08-06 06:13:11

不相关,但

if(i > sizeof(value)-1)
                {
                        return;
                }

应该是

if(i == sizeof(value)/sizeof(value[0]) )
                {
                        return;
                }

如果其他代码片段以错误的方式进行溢出检查并且因此它们覆盖了字符串的一部分,则可能是问题的原因

Not related, but

if(i > sizeof(value)-1)
                {
                        return;
                }

should be

if(i == sizeof(value)/sizeof(value[0]) )
                {
                        return;
                }

May be the cause of the problem if other pieces of code do the overflow checking in the wrong way and because of that they overwrite part of your string

半窗疏影 2024-08-06 06:13:11

我刚刚尝试在我自己的系统上编译并运行您的示例代码。 输出是正确的(即“8”出现在输出字符串中应有的位置),这表明在您提供给我们的代码范围之外还发生了其他事情。

我要冒险说你的一个变量或函数正在破坏你的输入字符串或其他一些变量或数组。 SendLine 和 SetVariable 是值得一看的地方。

但更重要的是,您没有向我们提供帮助您解决问题的工具。 当要求人们帮助您调试程序时,提供一个带有完整源代码的简单测试用例,以说明问题。 否则,我们只能猜测问题是什么,这对我们来说是令人沮丧的,对您来说也是没有成效的。

I've just tried compiling and running your sample code on my own system. The output is correct (i.e. '8' appears where it should be in the output string) which indicates to me that something else is going on outside of the scope of the code you've provided to us.

I'm going to go out on a limb and say that one of your variables or functions is trampling your input string or some other variable or array. SendLine and SetVariable are places to look.

But more importantly, you haven't given us the tools to help you solve your problem. When asking people to help you debug your program, provide a simple test case, with full source, that exemplifies the problem. Otherwise, we're left to guess what the problem is, which is frustrating for us and unproductive for you.

不必了 2024-08-06 06:13:11

atoi 对于无法呈现为数字的内容返回 0 —— 这只是一种预感,但是您是否尝试过转储字符串的二进制表示形式(或者甚至检查字符串长度是否匹配)?

atoi returns 0 for something that it can't render as numeric -- this is just a hunch, but have you tried dumping the binary representation of the string (or even checking that the string lengths match up)?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文