wstr and str

来源:互联网 发布:米表cms 编辑:程序博客网 时间:2024/06/05 17:40
string wstr2str(const wstring& wstr)
{//  convert the wstring into string
    
string curLocale = setlocale(LC_ALL, NULL); 
    setlocale(LC_ALL, 
"chs");
    
    
char *buff = (char*) malloc(sizeof(char* (wstr.size() * 2 + 1));
    wcstombs(buff, wstr.c_str(), wstr.size() 
* 2 + 1);
    
string str(buff);
    free(buff);
    
    setlocale(LC_ALL, curLocale.c_str());
    
return str;
}

wstring str2wstr(string &str)
{//  convert the string into wstring
    string curLocale = setlocale(LC_ALL, NULL);
    setlocale(LC_ALL, 
"chs");
    
    wchar_t 
*buff = (wchar_t*) malloc(sizeof(wchar_t) * (str.size() + 1));
    mbstowcs(buff, str.c_str(), str.size() 
+ 1);
    wstring wstr(buff);
    free(buff);
    
    setlocale(LC_ALL, curLocale.c_str());
    
return wstr;
}
原创粉丝点击