How to convert a string to integer in C?

后端 未结 12 1564
清酒与你
清酒与你 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:48

    Just wanted to share a solution for unsigned long aswell.

    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;
    }
    

提交回复
热议问题