本文主要解析cstring和int,double,char*之间的相互转换方法:
cout<<"测试1:int to cstring"<<endl;
int num=19;
CString cstr;
cstr.Format(_T("%4d"),num);
wcout<<cstr.GetString()<<endl;
cout<<"测试2:float to cstring"<<endl;
float f1=19.3234876;
cstr.Format(_T("%.2f"),f1);
wcout<<cstr.GetString()<<endl;
cout<<"测试3:double to cstring"<<endl;
double d1=19.3234876;
cstr.Format(_T("%.8lf"),d1);
wcout<<cstr.GetString()<<endl;
cout<<"测试4:16进制 to cstring"<<endl;
int num2=10;
cstr.Format(_T("%x"),num2);
wcout<<cstr.GetString()<<endl;
cout<<"测试5:char to cstring"<<endl;
char ch[10]="中国ABC";
cstr.Format(_T("%S"),ch);
wcout<<cstr.GetString()<<endl;
cout<<"测试6:cstring to int"<<endl;
CString cstrnum=_T("9987");
num=_ttoi(cstrnum);
cout<<num<<endl;
cout<<"测试7:cstrign to double"<<endl;
CString cstrdou=_T("987.32673");
double d2= _wtof(cstrdou);
cout<<d2<<endl;
cout<<"测试8:cstrign to char*"<<endl;
cstr=L"你好ABc";
string s=Unicode_To_Ansi(cstr.GetString());
cout<<s<<endl;
CString转std::wstring
std::wstring str = filename.GetString();
std::wstring转CString
CString str( filename.c_str() );