直接上代码:
string cstr2str(const CString &cStr)
{char pszResult[2048] = {0};
int n = cStr.GetLength();
int len = WideCharToMultiByte(CP_ACP,0,cStr,cStr.GetLength(),NULL,0,NULL,NULL);
WideCharToMultiByte(CP_ACP,0,cStr,cStr.GetLength(),pszResult,len,NULL,NULL);
pszResult[len+1] = '\0';
return pszResult;
}
CString str2cstr(const string &str)
{
WCHAR wszResult[2048] = {0};
int len = MultiByteToWideChar(CP_ACP,0,str.c_str(),str.length(),NULL,0);
MultiByteToWideChar(CP_ACP,0,str.c_str(),str.length(),wszResult,len);
wszResult[len+1] = '\0';
return wszResult;
}
本文提供了一段示例代码,展示了如何在C++中实现CString类型与标准string类型的相互转换。通过WideCharToMultiByte和MultiByteToWideChar函数,文章详细解释了字符集之间的转换过程。
520

被折叠的 条评论
为什么被折叠?



