C++字符串编码转换

static std::string ws2s(const std::wstring& ws)
{
    size_t convertedChars = 0;
    std::string curLocale = setlocale(LC_ALL, NULL);
    setlocale(LC_ALL, "chs");
    const wchar_t* wcs = ws.c_str();
    size_t dByteNum = sizeof(wchar_t)*ws.size() + 1;
    char* dest = new char[dByteNum];
#ifndef POP_WASM
    wcstombs_s(&convertedChars, dest, dByteNum, wcs, _TRUNCATE);
#else
    wcstombs(dest,wcs,dByteNum);
#endif
    std::string result = dest;
    delete[] dest;
    setlocale(LC_ALL, curLocale.c_str());
    return result;
}
static std::wstring s2ws(const std::string& s)
{
    size_t convertedChars = 0;
    std::string curLocale = setlocale(LC_ALL, NULL);
    setlocale(LC_ALL, "chs");
    const char* source = s.c_str();
    size_t charNum = sizeof(char)*s.size() + 1;
    wchar_t* dest = new wchar_t[charNum];
#ifndef POP_WASM
    mbstowcs_s(&convertedChars, dest, charNum, source, _TRUNCATE);
#else
    mbstowcs(dest, source, charNum);
#endif
    std::wstring result = dest;
    delete[] dest;
    setlocale(LC_ALL, curLocale.c_str());
    return result;
}
static std::string utf8ToStr(const std::string& utf8_str)
{
    std::wstring_convert> cvt;
    std::wstring ws = cvt.from_bytes(utf8_str);
    std::string ret = ws2s(ws);
    return ret;
}
static std::string StrToutf8(const std::string& str)
{
    std::wstring ws = s2ws(str);
    std::wstring_convert> cvt;
    std::string ret = cvt.to_bytes(ws);
    return ret;
}