std::string CStringToSTDStr(const CString& theCStr)
{
// Convert the CString to a regular char array
const int theCStrLen = theCStr.GetLength();
char *buffer = (char*)malloc(sizeof(char)*(theCStrLen+1));
memset((void*)buffer, 0, sizeof(buffer));
WideCharToMultiByte(CP_UTF8, 0, static_cast<cstring>(theCStr).GetBuffer(), theCStrLen, buffer, sizeof(char)*(theCStrLen+1), NULL, NULL);
// Construct a std::string with the char array, free the memory used by the char array, and
// return the std::string object.
std::string STDStr(buffer);
free((void*)buffer);
return STDStr;
}将MFC的CString 类型转化为C++标准数据类型std::string
最新推荐文章于 2022-04-07 20:25:41 发布
本文介绍了一个将C++的CString类型转换为std::string类型的函数实现,包括内存分配、字符集转换及资源释放。

934

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



