CString转string
CString strMfc=L“test“;std::string strStl;
strStl=strMfc.GetBuffer(0);
在unicode编码下CString转string如下:
CString strMfc=L“test“;
std::string str = CStringA(strMfc);
std::string转CString
CString strMfc;
std::string strStl=“test“;strMfc=strStl.c_str();
或者直接CString strMfc(strStl.c_str());
string转char*
string str="ddd";
str.c_str();
WCHAR 和 char转换
void WCharToChar(WCHAR* wbuf, char* buf,int length)
{
WideCharToMultiByte(CP_UTF8, 0, wbuf, -1, buf, length, NULL, NULL);
}
void CharToWChar(WCHAR* wbuf, char* buf, int length)
{
MultiByteToWideChar(CP_UTF8, 0, buf, -1, wbuf, length);
}
int _tmain(int argc, _TCHAR* argv[])
{
int length = 0;
WCHAR *wBuf = L"ABC DEF";
length = wcslen(wBuf)+1;
char *buf = new char[length];
WCharToChar(wBuf, buf, length);
printf("char:%s\n", buf);
length = strlen(buf) + 1;
WCHAR *wBufR = new WCHAR[length];
memset(wBufR, 0, sizeof(WCHAR)*length);
CharToWChar(wBufR, buf, length);
wprintf(L"WCHAR:%s\n", wBufR);
system("pause");
return 0;
}