运行结果:
#include using namespace std; long long f_AtoI(char str[]) // 字符串转为整数,通过减'0'字符,底层用ASCII码相减 { int i = 0; long long tempI = 0; while (str[i] != '\0') { tempI = tempI * 10 + (str[i] - '0'); // 与字符0的差距 // cout << "i=" << i << " Get=" << (str[i] - '0') << endl; ++i; } return tempI; // 转换后赋值给m } char *f_ItoA(long long n) { static char tmpStr[100]{0}; static char str[]{0}; int i(0); int j = 0; while (n) { tmpStr[i++] = n % 10 + '0'; n /= 10; } // 此时为逆序,需要调整为正序 // cout << "tmpStr=" << tmpStr << endl; while (i > 0) str[j++] = tmpStr[--i]; // cout << "str=" << str << endl; return str; } int main() { char str[] = "5000000050000000"; long long llngN = 0; llngN = f_AtoI(str); cout << "llngN=" << llngN << endl; string str2 = f_ItoA(llngN); cout << "str2=" << str2 << endl; system("pause"); return 0; }
llngN=5000000050000000 str2=5000000050000000 请按任意键继续. . .