多字节和宽字节的互相转换函数
2014-03-19 23:20:14 liuchen110951 阅读数 415
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/liuchen110951/article/details/21564123
string UnicodeToWideChar(const wstring &str)
{
int nLen = WideCharToMultiByte(CP_ACP,0,str.c_str(),-1,NULL,0,NULL,NULL);
char *pStr = new char[nLen];
memset(pStr,0,sizeof(char)*nLen);
WideCharToMultiByte(CP_ACP,0,str.c_str(),-1,pStr,nLen,NULL,NULL);
string strText = pStr;
delete[] pStr;
pStr = NULL;
return strText;
}
wstring WideCharToUnicode(const string &str)
{
int nLen = MultiByteToWideChar(CP_ACP,0,str.c_str(),-1,NULL,0);
wchar_t *pStr = new wchar_t[nLen];
memset(pStr,0,sizeof(wchar_t)*nLen);
MultiByteToWideChar(CP_ACP,0,str.c_str(),-1,pStr,nLen);
wstring strText = pStr;
delete[] pStr;
pStr = NULL;
return strText;
}