最近写程序老师碰到字符串之间的转换,每次都记不住还是写个笔记记一下要不每次都查半天太费劲
CString 与 string之间的转换
//下面的两种方法VS2015都有错
//1.有错CString -》string
//CString str = finder.GetFileName();
//string strPath = W2A(str);//str.GetBuffer(0);
//2.VS2008没错CString -》string
//string strPath = str.GetBuffer(0);
//string strPath = (LPCSTR)(finder.GetFilePath());
//3.VS2015没有错CString -》string
USES_CONVERSION;
string strFileStr = W2A(m_strFind.GetBuffer());
//4.VS2015没有错 string ->CString
CString str = (LPCTSTR)strFilePath.c_str();//乱码
char ch[2048] = "";
sprintf(ch, "%s", strFilePath.c_str());
int num = MultiByteToWideChar(0, 0, ch, -1, NULL, 0);
wchar_t *lpstr1 = new wchar_t[num];
MultiByteToWideChar(0, 0, ch, -1, lpstr1, num);
/* sprintf(ch, "%s", finder.GetFileName());//只能读取首字符
用构造函数,参数传由cstring转换来的char* :
sring a( (LPSTR)(LPCTSTR)cstring );
string strPath = ch;*/
向wchar中赋值
MultiByteToWideChar(CP_ACP, 0, "测试程序", -1, strWchar, 128);