CString 转 std::string

USES_CONVERSION;
    CString str = L"sldfjsl";
    CT2A xx(str);

    string ss = xx



CString、string 和char* 之间的转换总结


char*   转成CString

Ø 可以直接构造.如下:   

char   *   p   =   "test"; 

CString   str(p); 

Ø 可以用成员函数Format   如下: 

char*   p   =   "test”; 

CString   str; 

str.Format("%s",   p);

char* 转换成string

直接从char*构造出string就可以了

                                             chat* sz= "cwsasd";

                                             string s1(sz);

string转换成char*

char *tp = const_cast(s1.c_str());

string转换成CString

CString str2;

str2.Format(L"%s", s1.c_str());

CString 转换成char*

CString str("test");

int len=str.getLength();

char *cstr=new char[len+1];

cstr[len]=0;

WideCharToMultiByte(CP_OEMCP, 0, str, -1, cstr, len, NULL, NULL);

CString 转换成string

// 方法:【先将CString转换成char*, 然后从char*构造出string】

CString t = CString(s[i]);

int len=t.GetLength();

char *cstr=new char[len+1];

cstr[len]=0;

WideCharToMultiByte(CP_OEMCP, 0, t, -1, cstr, len, NULL, NULL);

string s1(cstr);

你可能感兴趣的:(CString 转 std::string)