标签:
现在都用宽字节(unicode)了,所以建议使用wstring取代string(ansi版)
不像CString一样有Format函数去转换,string/wsting类型需要手动转换
#include <strsafe.h> #include <sstream> using namespace std; using std::endl; using std::wcout; using std::wstring; wstring int2string() { wstring strNum; int nNum = 1039; TCHAR szNum[16] = { 0 }; std::wcout.imbue(std::locale("chinese")); //让wout可以输出中文; //方法1 _itoa_s/_itow_s法 _itow_s(nNum, szNum, 10); strNum = szNum; wcout <<L"测试语句,方法1: " <<strNum.c_str()<< endl; //方法2 StringCchPrintf/sprintf法 strNum.clear(); StringCchPrintf(szNum, 16, _T("%d"), nNum); strNum = szNum; wcout << L"测试语句,方法2: " << strNum.c_str() << endl; //方法3 stringstream/wstringstream法 strNum.clear(); wstringstream strNum2; strNum2 << nNum; strNum2 >> strNum; wcout << L"测试语句,方法3: " << strNum.c_str() << endl; return strNum; }
标签:
原文地址:http://www.cnblogs.com/Lthis/p/4199162.html