How to convert a string to integer in C?

后端 未结 12 1571
清酒与你
清酒与你 2020-11-22 01:40

I am trying to find out if there is an alternative way of converting string to integer in C.

I regularly pattern the following in my code.

char s[] =         


        
相关标签:
12条回答
  • 2020-11-22 01:59

    Don't use functions from ato... group. These are broken and virtually useless. A moderately better solution would be to use sscanf, although it is not perfect either.

    To convert string to integer, functions from strto... group should be used. In your specific case it would be strtol function.

    0 讨论(0)
  • 2020-11-22 01:59
    int atoi(const char* str){
        int num = 0;
        int i = 0;
        bool isNegetive = false;
        if(str[i] == '-'){
            isNegetive = true;
            i++;
        }
        while (str[i] && (str[i] >= '0' && str[i] <= '9')){
            num = num * 10 + (str[i] - '0');
            i++;
        }
        if(isNegetive) num = -1 * num;
        return num;
    }
    
    0 讨论(0)
  • 2020-11-22 01:59
    //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;
    }
    
    0 讨论(0)
  • 2020-11-22 02:01

    You can code atoi() for fun:

    int my_getnbr(char *str)
    {
      int result;
      int puiss;
    
      result = 0;
      puiss = 1;
      while (('-' == (*str)) || ((*str) == '+'))
      {
          if (*str == '-')
            puiss = puiss * -1;
          str++;
      }
      while ((*str >= '0') && (*str <= '9'))
      {
          result = (result * 10) + ((*str) - '0');
          str++;
      }
      return (result * puiss);
    }
    

    You can also make it recursive, which can fold in 3 lines. =)

    0 讨论(0)
  • 2020-11-22 02:03

    There is strtol which is better IMO. Also I have taken a liking in strtonum, so use it if you have it (but remember it's not portable):

    long long
         strtonum(const char *nptr, long long minval, long long maxval,
         const char **errstr);
    

    EDIT

    You might also be interested in strtoumax and strtoimax which are standard functions in C99. For example you could say:

    uintmax_t num = strtoumax(s, NULL, 10);
    if (num == UINTMAX_MAX && errno == ERANGE)
        /* Could not convert. */
    

    Anyway, stay away from atoi:

    The call atoi(str) shall be equivalent to:

    (int) strtol(str, (char **)NULL, 10)
    

    except that the handling of errors may differ. If the value cannot be represented, the behavior is undefined.

    0 讨论(0)
  • 2020-11-22 02:11

    Yes, you can store the integer directly:

    int num = 45;
    

    If you must parse a string, atoi or strol is going to win the "shortest amount of code" contest.

    0 讨论(0)
提交回复
热议问题