VS2008下,CString與string互轉換,LPTSTR轉CString,char * 轉LPTSTR

   這個問題很糾結,由於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);

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章