当前位置: 代码迷 >> 综合 >> C++/MFC-CString,String,CHAR*互相转换
  详细解决方案

C++/MFC-CString,String,CHAR*互相转换

热度:46   发布时间:2023-12-14 05:48:51.0

CString CString1;

string string1;

CHARchar1=NULL;

一、CString转char*,string

//1。CString转string
string1=CString1.GetBuffer();
CString1.ReleaseBuffer();
//2。CString转CHAR*
char1=CString1.GetBuffer();
CString1.ReleaseBuffer();

二、string转char*,CString

//1。string转CString
CString1=string1.data();
//使用多字节编码
//2。sting转CHAR*
char1=new CHAR[string1.length()+1];
strcpy(char1,string1.data());

//使用Unicode编码
std::wstring string1 = _T("string");
WCHAR * char1 = _T("char1");
//2。sting转CHAR*
char1 = new WCHAR[string1.length() + 1];
wcscpy(char1,string1.data());

三、char* 转CString ,string
char1=_T("char1");
CString1=char1;
string1=char1;






  相关解决方案