在 C 中使用 strtol 或 strtok 解析字符串?

发布于 2024-08-24 12:57:11 字数 524 浏览 6 评论 0原文

字符串输入是

> bc <address1> <address2> length

我可以使用 strtok 将字符串分解为标记,但不确定如何获取每个单独的标记,例如将地址 1 和地址 2 转换为十六进制。

void tokenize()
{
char str[] ="bc 0xFFFF0 0xFFFFF 30";
char *tkn;
char *tkn2;

tkn = strtok (str," ");  

  while (tkn != NULL) {

    while (*tkn != 0)
    {
        putchar(*tkn);
        *tkn++;
    }
  tkn = strtok (NULL, " ");
  printf("\n"); 
  }
}

到目前为止,它打印了令牌,但我不确定如何单独使用每个令牌。

bc
0x000FF
0x0FFF
30

The string input would be

> bc <address1> <address2> length

I can break the string into tokens using strtok but not sure how to take each separate token and for example convert address1 and address 2 to hex.

void tokenize()
{
char str[] ="bc 0xFFFF0 0xFFFFF 30";
char *tkn;
char *tkn2;

tkn = strtok (str," ");  

  while (tkn != NULL) {

    while (*tkn != 0)
    {
        putchar(*tkn);
        *tkn++;
    }
  tkn = strtok (NULL, " ");
  printf("\n"); 
  }
}

So far it prints the tokens but I am not sure how to use each separately.

bc
0x000FF
0x0FFF
30

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

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

发布评论

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

评论(4

乖乖哒 2024-08-31 12:57:11

使用 strtol 转换数字。第三个参数是基数,特殊值 0 将告诉 strtol 根据“0x”等内容进行猜测。

long num;
char s[] = "0xFFFF0";
char s2[] = "30";

num = strtol(s, &endptr, 0);
// if s==endptr, there was an error.
// if you really want to be complete, endptr - s should equal strlen("0xFFFF0")

num = strtol(s2, &endptr, 0);
// Same error checks.

Use strtol to convert the numbers. The third argument is the base and the special value of 0 will tell strtol to guess based on things like "0x".

long num;
char s[] = "0xFFFF0";
char s2[] = "30";

num = strtol(s, &endptr, 0);
// if s==endptr, there was an error.
// if you really want to be complete, endptr - s should equal strlen("0xFFFF0")

num = strtol(s2, &endptr, 0);
// Same error checks.
你的心境我的脸 2024-08-31 12:57:11

如果字符串的格式是固定并且您想将位置23的十六进制转换为数字,您可以尝试类似的方法:

    char str[] ="bc 0xFFFF0 0xFFFFF 30";
    int count = 1;
    int n1,n2;
    char *tkn= strtok (str," ");
    while (tkn != NULL) {

            if(count == 2) { // tkn now points to a string "0xFFF0", now convert to int.
                    sscanf(tkn,"%x",&n1);
            }
            if(count == 3) {
                    sscanf(tkn,"%x",&n2); break;
            }
            tkn = strtok (NULL, " ");
            count++;
    }
    printf("%x %x\n",n1,n2); // prints ffff0 fffff

If the format of the string is fixed and you want to convert the hex which are in position 2 and 3 into numbers you can try something like:

    char str[] ="bc 0xFFFF0 0xFFFFF 30";
    int count = 1;
    int n1,n2;
    char *tkn= strtok (str," ");
    while (tkn != NULL) {

            if(count == 2) { // tkn now points to a string "0xFFF0", now convert to int.
                    sscanf(tkn,"%x",&n1);
            }
            if(count == 3) {
                    sscanf(tkn,"%x",&n2); break;
            }
            tkn = strtok (NULL, " ");
            count++;
    }
    printf("%x %x\n",n1,n2); // prints ffff0 fffff
笑红尘 2024-08-31 12:57:11

以下只是草图。

尝试

char * addresses[ 2 ];
i = 0;

然后,在 while 循环内部

strncpy( addresses[ i ], *tkn, , MAX_TOKEN_LENGTH );
++i;

和循环外部,可以通过访问 地址[] 数组来使用输入。

Following is a sketch only.

Try

char * addresses[ 2 ];
i = 0;

Then, inside the while loop

strncpy( addresses[ i ], *tkn, , MAX_TOKEN_LENGTH );
++i;

Outside the loop, the input can be used by accessing the addresses[] array.

深居我梦 2024-08-31 12:57:11

嗯...我想我只需使用 sscanf:

int address1, address2, num;

sscanf(input_string, "%*s %i %i %i", &address1, &address2, &num);

“%*s”读取并忽略第一个字符串(“bc”)。每个后续的 %i 读取并转换一个整数,使用正常的 C 风格约定,如果数字以“0x”开头,它将被转换为十六进制,否则如果有一个前导“0”,它将被转换为八进制,否则将转换为十进制。

Hmm...I think I'd just use sscanf:

int address1, address2, num;

sscanf(input_string, "%*s %i %i %i", &address1, &address2, &num);

The "%*s" reads and ignores the first string (the "bc"). Each succeeding %i reads and converts one integer, using the normal C-style convention that if the number starts with "0x", it'll be converted as hexadecimal, otherwise if there's a leading "0", it'll be converted as octal, and otherwise it'll be converted as decimal.

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