Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
计算字符串和数组-C_C_Arrays_String - Fatal编程技术网

计算字符串和数组-C

计算字符串和数组-C,c,arrays,string,C,Arrays,String,这是用于C编程的。我正在准备期中考试,实践测试中的一个问题让我有点困惑,希望有人能帮助我了解代码 该函数的代码为: int xtoi(char s[]) { int i; int result; i = result = 0; while (s[i]) { if (s[i] >= '0' && s[i] <= '9') { result = result * 16 + s[i++] - '0';

这是用于C编程的。我正在准备期中考试,实践测试中的一个问题让我有点困惑,希望有人能帮助我了解代码

该函数的代码为:

int xtoi(char s[])
{
    int i;
    int result;

    i = result = 0;
    while (s[i]) {
        if (s[i] >= '0' && s[i] <= '9') {
            result = result * 16 + s[i++] - '0';
        } else if (s[i >= 'a' && s[i] <= 'f') {
            result = result * 16 + s[i++] - 'a' + 10;
        } else if (s[i] >= 'A' && s[i] <= 'F') {
            result = result * 16 + s[i++] - 'A' +10;
        }
    }
    return result;
}
答案应该是506,但我不知道如何回答。这可能很简单,但这对我来说都是比较新的,所以非常感谢您的帮助和指导


提前感谢。

该函数将十六进制字符串转换为
int
s。
atoi
是一个很好的选择

int xtoi(char s[])
{
    int i = 0;
    int result = 0;

    /* while we haven't seen the null terminator */
    while (s[i]) {
        /* if this char is a digit
         * convert this char to an int and then add
         * it to the next decimal place in the number.
         * Then advance to the next character */
        if (s[i] >= '0' && s[i] <= '9')
            result = result * 16 + s[i++] - '0';                   

        /* if this character is a hex digit
         * convert this char to an int and then add 
         * it to the next decimal place in the number.
         * Then advance to the next character */
        else if (s[i] >= 'a' && s[i] <= 'f')
            result = result * 16 + s[i++] - 'a' + 10;

        /* same as above, except for uppercase hex digits */
        else if (s[i] >= 'A' && s[i] <= 'F')
            result = result * 16 + s[i++] - 'A' + 10;
    }
    /* return the converted number */
    return result;
}
xtoi(“1fa”)的输出正确:506

    • 如果
      s[i]
      是从“0”到“9”的
      char
      ,请将其转换为 相应的
      int
      (0到9)
    • 如果不是,如果
      s[i]
      是从'a'到'f'的
      char
      ,则将其转换为相应的
      int
      (10到16)
    • 如果不是,如果
      s[i]
      是从'a'到'F'的
      char
      ,则将其转换为相应的
      int
      (10到16)
    • 如果仍然没有,忽略它
  • 然后将所有这些数字相加(遵循规则)以生成由
    s
    表示的十六进制值

  • 关于规则的提示:例如,假设您想要得到一个由“4”和“2”组成的十进制数。首先让临时结果为4,乘以10,然后在临时结果上加2。这会给你你想要的:4*10+2=42

    请再想一想,我保证你能自己理解



    顺便说一句,
    xtoi(“1fa”)
    的结果与
    strtol(“1fa”,NULL,16)的结果相同

    这太宽泛了。如果你对某些特定的问题有疑问,你应该缩小范围,启动调试器,逐行跟踪程序。注意所有的局部变量(最好是十六进制)。谢谢大家,是的,如果你们觉得自己很愚蠢,在头脑清醒的情况下回到这里,发现它只是十六进制到十进制的转换。使用的是Ascii码,这是错误的,哈哈。
    int xtoi(char s[])
    {
        int i = 0;
        int result = 0;
    
        /* while we haven't seen the null terminator */
        while (s[i]) {
            /* if this char is a digit
             * convert this char to an int and then add
             * it to the next decimal place in the number.
             * Then advance to the next character */
            if (s[i] >= '0' && s[i] <= '9')
                result = result * 16 + s[i++] - '0';                   
    
            /* if this character is a hex digit
             * convert this char to an int and then add 
             * it to the next decimal place in the number.
             * Then advance to the next character */
            else if (s[i] >= 'a' && s[i] <= 'f')
                result = result * 16 + s[i++] - 'a' + 10;
    
            /* same as above, except for uppercase hex digits */
            else if (s[i] >= 'A' && s[i] <= 'F')
                result = result * 16 + s[i++] - 'A' + 10;
        }
        /* return the converted number */
        return result;
    }
    
    int xtoi(char s[])
    {
        int i = 0;
        int result = 0;
    
        /* while we haven't seen the null terminator */
        for (i = 0; s[i]; ++i) {
            /* if this char is a digit
             * convert this char to an int and then add
             * it to the next decimal place in the number. */
            if (isdigit(s[i]))
                result = result * 16 + (s[i] - '0');
    
            /* if this character is a hex digit
             * convert this char to an int and then add
             * it to the next decimal place in the number. */
            else if (isxdigit(s[i])) {
                /* if the hex digit is uppercase, the char to subtract
                 * from is also uppercase. Otherwise it is lowercase */
                char base = isupper(s[i]) ? 'A' : 'a';
                result = result * 16 + s[i] - base + 10;
            }
        }
        /* return the converted number */
        return result;
    }