我试图找出是否有一种在C中转换字符串为整数的替代方法。

在我的代码中,我经常使用以下模式。

char s[] = "45";

int num = atoi(s);

那么,有没有更好的办法呢?


当前回答

//I think this way we could go :
int my_atoi(const char* snum)
{
 int nInt(0);
 int index(0);
 while(snum[index])
 {
    if(!nInt)
        nInt= ( (int) snum[index]) - 48;
    else
    {
        nInt = (nInt *= 10) + ((int) snum[index] - 48);
    }
    index++;
 }
 return(nInt);
}

int main()
{
    printf("Returned number is: %d\n", my_atoi("676987"));
    return 0;
}

其他回答

正如前面提到的,任何C程序都不应该使用atoi函数族,因为它们没有任何错误处理。

strtol系列函数是100%等效的,但具有扩展功能:它具有错误处理功能,并且还支持十进制以外的其他进制,例如十六进制或二进制。因此正确的答案是:使用strtol (family)。

如果出于某种原因,您坚持自己手动推出这个函数,那么您应该尝试执行类似于strotol的操作,以防出现除可选符号和数字之外的其他符号。例如,我们想要转换大字符串中的数字是很常见的。

具有错误处理支持的简单版本可能如下所示。这段代码仅适用于十进制进制10的数字,但在其他方面表现类似于strtol,其可选指针设置为指向遇到的第一个无效符号(如果有的话)。还要注意,这段代码不处理溢出。

#include <ctype.h>

long my_strtol (char* restrict src, char** endptr)
{
  long result=0;
  long sign=1;

  if(endptr != NULL) 
  {
    /* if input is ok and endptr is provided, 
       it will point at the beginning of the string */
    *endptr = src;
  }

  if(*src=='-')
  {
    sign = -1;
    src++;
  }

  for(; *src!='\0'; src++)
  {
    if(!isdigit(*src)) // error handling
    {
      if(endptr != NULL)
      {
        *endptr = src;
      }
      break;
    }
    result = result*10 + *src - '0';
  }

  return result * sign;
}

例如,要处理溢出,可以添加代码来计数字符并检查它们是否超过10,假设长度为32位,最大可以是2147483647,10位数字。

是的,你可以直接存储整数:

int num = 45;

如果你必须解析一个字符串,atoi或strol将赢得“最短代码量”的比赛。

只是想分享一个unsigned long的解决方案。

unsigned long ToUInt(char* str)
{
    unsigned long mult = 1;
    unsigned long re = 0;
    int len = strlen(str);
    for(int i = len -1 ; i >= 0 ; i--)
    {
        re = re + ((int)str[i] -48)*mult;
        mult = mult*10;
    }
    return re;
}

你可以自己卷!

#include <stdio.h>
#include <string.h>
#include <math.h>

int my_atoi(const char* snum)
{
    int idx, strIdx = 0, accum = 0, numIsNeg = 0;
    const unsigned int NUMLEN = (int)strlen(snum);

    /* Check if negative number and flag it. */
    if(snum[0] == 0x2d)
        numIsNeg = 1;

    for(idx = NUMLEN - 1; idx >= 0; idx--)
    {
        /* Only process numbers from 0 through 9. */
        if(snum[strIdx] >= 0x30 && snum[strIdx] <= 0x39)
            accum += (snum[strIdx] - 0x30) * pow(10, idx);

        strIdx++;
    }

    /* Check flag to see if originally passed -ve number and convert result if so. */
    if(!numIsNeg)
        return accum;
    else
        return accum * -1;
}

int main()
{
    /* Tests... */
    printf("Returned number is: %d\n", my_atoi("34574"));
    printf("Returned number is: %d\n", my_atoi("-23"));

    return 0;
}

这样就可以做你想做的事情,而且不会杂乱。

//I think this way we could go :
int my_atoi(const char* snum)
{
 int nInt(0);
 int index(0);
 while(snum[index])
 {
    if(!nInt)
        nInt= ( (int) snum[index]) - 48;
    else
    {
        nInt = (nInt *= 10) + ((int) snum[index] - 48);
    }
    index++;
 }
 return(nInt);
}

int main()
{
    printf("Returned number is: %d\n", my_atoi("676987"));
    return 0;
}