多字节和宽字节的互相转换函数

来源:互联网 发布:开元盛世 知乎 编辑:程序博客网 时间:2024/05/22 23:58

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;
}

0 0
原创粉丝点击