简体   繁体   English

解析C中来自字符串的两个字符输入

[英]Parsing two character input from a string in C

Okay so I am parsing a 18 character string consisting of '?'s and '0' - '9'. 好的,所以我正在解析由'?'和'0'-'9'组成的18个字符串。 What I am trying to do is use the atoi function to convert two character chunks of the string into integers. 我正在尝试使用atoi函数将字符串的两个字符块转换为整数。 The characters that I need to parse are in an array. 我需要解析的字符在一个数组中。 I am lost on how to implement such a solution. 我不知道如何实施这样的解决方案。

  char *str = "01";
  int n = atoi(str);
  printf("The string %s as an integer is = %d\n",str,n);

Which gives you "The string 01 as an integer is = 1" 给出“字符串01作为整数是= 1”

Here's something that may work. 这可能有效。

  1. Advance pointer to next decimal or end of string 前进指针到字符串的下一个小数或结尾
  2. If reached end of string, you're done 如果到达字符串末尾,就完成了
  3. Use strtol , keep the result. 使用strtol ,保留结果。 This will advance the pointer to the next non-decimal or the end of the string. 这会将指针前进到下一个非十进制数或字符串的末尾。
  4. Go back to step 1. 返回步骤1。

Here's some source. 这是一些资料。

#include <stdlib.h>
#include <stdio.h>
#define ARR_LEN 18    
int
main(void)
{
    char *str = "12?456?8??12????78";
    char *ptr = str;
    int result[ARR_LEN];
    int i = 0;
    int j = 0;

    i = 0;
    for (; ;)
    {
        while (*ptr == '?' && *ptr != '\0')
            ++ptr;
        if (*ptr == '\0')
            break;
        result[i++] = (int)strtol(ptr, &ptr, 10);
    }    
    for (j = 0; j < i; ++j)
        printf("%d ", result[j]);
    printf("\n");
    return 0;
}
#define UNKNOWN_CONTROL -1           // integer to recognize unknown pairs
#define CONSTANT_STRING_LENGTH 18    // The string length

int i;
char string[CONSTANT_STRING_LENGTH]; // This is your string
int pairs[CONSTANT_STRING_LENGTH/2]; // Array to store results

for (i=0; i<CONSTANT_STRING_LENGTH/2; i++) {       // For each pair in the string
  if (string[i*2] == '?' || string[i*2]+1 == '?')  // Is it a '??' pair?
    pairs[i] = UNKNOWN_CONTROL;                    // Store some constant (> 99)
  else
    pairs[i] = string[i*2] * 10 + string[i*2+1] - '0' * 11; // Compute the number and store
}

And then your array pairs would be filled with your desired results if I got your question right. 如果我的问题正确,那么您的数组将充满您想要的结果。

EDIT: To understand the computing bit, you've got to understand that ASCII characters (printable, the ones you store on strings) don't correspond to their integer counterparts. 编辑:要了解计算位,您必须了解ASCII字符(可打印的,存储在字符串上的ASCII字符)与整数对应。 ASCII '0' is integer 48, ASCII '1' is integer 49, ASCII '2' is integer 50, and forth... ASCII'0'是整数48,ASCII'1'是整数49,ASCII'2'是整数50,等等...

By multiplying the first character by ten and adding the first characters, you're summing the ASCII values, not the integer ones, so you've got to subtract the bias. 通过将第一个字符乘以10并添加第一个字符,就可以得出ASCII值而不是整数值,因此必须减去偏差。 Substracting '0' (the base number for ASCII) would work for one character (eg ASCII '2' - ASCII '0' == 2 ), but you've got to multiply it by 11 for two characters. 减去“ 0”(ASCII的基数)将对一个字符有效(例如ASCII'2'-ASCII'0'== 2 ),但是对于两个字符,您必须将其乘以11。

Keep in mind '0' * 11 is the same as '0' * 10 + '0'. 请记住,“ 0” * 11与“ 0” * 10 +“ 0”相同。 Doing some redistribution of the math you can see exactly what's being done there: 对数学进行一些重新分配,您可以确切地看到正在执行的操作:

pairs[i] = (string[i*2] - '0') * 10 + string[i*2+1] - '0';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM