how can I convert character to integer number

前端 未结 4 958
小蘑菇
小蘑菇 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 18:19

    I prefer to use the std::atoi() function to convert string into a number data type. In your example you have an non-zero terminated array "\0", so the function will not work with this code.

    Consider to use zero terminated "strings", like:

    char *a = "4534";
    
    int mynumber = std::atoi( a );   // mynumber = 4534
    

提交回复
热议问题