比如控制台下的程序:
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
//string 转换到 int
string a;
cout << "Input string:";
loop:
cin >> a;
int len = a.length(); //得到a的长度
for(int i = 0; i < len; i++)
{
if(a[i] < 48 || a[i] > 57) //ASCII码,0--9
break;
}
if(i < len)
{
cout << "数据不合法!" << endl;
goto loop;
}
char* temp = new char[len+1];
temp[len] = 0;
for(i = 0; i < len; i++)
{
temp[i] = a[i];
}
int num = atoi(temp);
delete temp;
cout << "Transfered:" << num << endl; //输出转换数据,int转换为char*的函数是itoa(),查一下MSDN就可以写出
}