how can I convert character to integer number

前端 未结 4 957
小蘑菇
小蘑菇 2021-01-07 17:15

How could I change an array of character (string) to an integer number without using any ready function (ex atoi();) for example :-

 char a[5]=\'4534\';
         


        
4条回答
  •  离开以前
    2021-01-07 17:55

    You can go like this

    It's working

    #include 
    #include 
    #include 
    
    int main()
    {
    
      char a[5] = "4534";
    
      int converted = 0;
    
      int arraysize = strlen(a);
    
      int i = 0;
      for (i = 0; i < arraysize ; i++)
      {
        converted = converted *10 + a[i] - '0';
      }
    
      printf("converted= %d", converted);   
    
      return 0;
    }
    

提交回复
热议问题