const wchar_t*转换成string类型
原创
©著作权归作者所有:来自51CTO博客作者303103757q的原创作品,请联系作者获取转载授权,否则将追究法律责任
直接上代码:
std::string CWTOA(const wchar_t* lpwcszWString)
{
char* pElementText;//定义一个char类型指针
int iTextLen;//定义长度
iTextLen = ::WideCharToMultiByte(CP_ACP, 0, lpwcszWString, -1, NULL, 0, NULL, NULL);//获取传入字符串长度 pElementText = new char[iTextLen + 1];//开辟空间
memset((void*)pElementText, 0, (iTextLen + 1) * sizeof(char));//清空 ::WideCharToMultiByte(CP_ACP, 0, lpwcszWString, -1, pElementText, iTextLen, NULL, NULL);//转换
std::string strReturn(pElementText);//定义string类型变量,以构造的方式传入字符串赋值
delete[] pElementText;
return strReturn;//返回你懂的;