這個問題很糾結,由於unicode的緣故,搞得我這個新手很無奈。還好解決了,現將方式寫下,希望以後能找到更好的方法。
CString->string (需要兩次轉換)
string C2S(CString cstr)
{
LPTSTR lpsz = new TCHAR[cstr.GetLength()+1];
_tcscpy(lpsz, cstr);
char *p=new char[(cstr.GetLength()+1)*2];
WideCharToMultiByte(CP_ACP, 0, lpsz, -1, p, strlen(p), NULL, NULL);
string str = p;
return str;
};
string-> CString (這個利用CString的構造函數)
CString S2C(string str)
{
CString cstr(str.c_str());
return cstr;
};
char *->LPTSTR
LPTSTR Char2LPTSTR(char *p)
{
int l=MultiByteToWideChar(CP_ACP,0,p ,-1,NULL,0);
LPTSTR s=new TCHAR[l];
MultiByteToWideChar(CP_ACP,0,p ,-1,s,l);
return s;
};
LPTSTR->CString(使用format)
LPTSTR str;
CString cstr;
cstr.Format(_T("%s"),str);