#include <stdlib.h> #include <stdio.h> int main(void) { int n = 0; char *str = "12345.67"; n = atoi(str); printf("string = %s integer = %d\n", str, n);
CString strTriNum = _T("123"); int iTriNum = 0; iTriNum = _tstoi(strTriNum); cout<<iTriNum<<endl; return 0; }
简单的实现atoi函数源代码:
#include <cctype> int my_atoi(const char* p) { assert(p != NULL); bool neg_flag = false;// 符号标记 int res = 0;// 结果 if(p[0] == '+' || p[0] == '-') neg_flag = (*p++ != '+'); while(isdigit(*p)) res = res*10 + (*p++ - '0'); return neg_flag ?0 -res : res; }
#include <cctype> int my_atoi(const char* p) { assert(p != NULL); bool neg_flag = false;// 符号标记 int res = 0;// 结果 if(p[0] == '+' || p[0] == '-') neg_flag = (*p++ != '+'); while(isdigit(*p)) res = res*10 + (*p++ - '0'); return neg_flag ?0 -res : res; }