1 CString 转 BSTR
CString s=pLayerName;
BSTR bstr=s.AllocSysString();
2 BSTR 转 CString
CString sName;
BSTR bstrName;
sName=(const TCHAR*)_bstr_t(bstrName);
3 CString转化为double型
double=atof(CString);
error C2664: “atof”: 不能将参数 1 从“CString”转换为“const char *”
解决:用_ttof代替atof 或 _wtof(str);
4 CString转换成char*
CString strSource;//宣告CString
char* charSource; //宣告char*
法1:
charSource = (char*)strSource.GetBuffer(0);
法2:
charSource = (char*)strSource.GetBuffer(strSource.GetLength());
法3:
charSource = (char*)(LPCTSTR)strSource;
法4:
std::string CStringToStdString(const CString& cstr)
{
int size = WideCharToMultiByte(CP_ACP, 0, cstr, -1, NULL, 0, NULL, NULL);
std::string str(size, 0);
WideCharToMultiByte(CP_ACP, 0, cstr, -1, &str[0], size, NULL, NULL);
return str;
}
charSource = CStringToStdString(strSource).c_str();
5、Unicode下CString转换为char *
使用函数:T2A、W2A
CString str = _T("D://校内项目//QQ.bmp");
//声明标识符
USES_CONVERSION;
//调用函数,T2A和W2A均支持ATL和MFC中的字符转换
char * pFileName = T2A(str);
//char * pFileName = W2A(str); //也可实现转换
注意:有时候可能还需要添加引用#include <afxpriv.h>
6、Unicode下char *转换为CString
char * pFileName = "D://校内项目//QQ.bmp";
USES_CONVERSION;
CString s = A2T(pFileName);
//CString s = A2W(pFileName);