WideCharToMultiByte:这是把UTF8转成Unicode了(宽字节转成多字节)。当然错了。

解决方案 »

  1.   

    //UTF8转换成UNICODE
    std::wstring utf8_to_unicode(const std::string& strdata) 
    {
    std::wstring wstrdata;
    if (strdata.size() == 0)
    return L""; wstrdata.resize(strdata.size() * 2); int nretcode = MultiByteToWideChar(CP_UTF8, 0, strdata.c_str(), strdata.length(), &wstrdata[0], wstrdata.size()); if (nretcode > 0)
    wstrdata.resize(nretcode); return wstrdata;
    }
    这才是UTF8转成Unicode
      

  2.   


    std::string UnicodeToANSI( std::wstring str ,UINT  CodePage)
    {
    char*     pElementText;
    int    iTextLen;
    // wide char to multi char
    iTextLen = WideCharToMultiByte( CodePage,
    0,
    str.c_str(),
    -1,
    NULL,
    0,
    NULL,
    NULL ); pElementText = new char[iTextLen + 1];
    memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) );
    ::WideCharToMultiByte(CodePage,
    0,
    str.c_str(),
    -1,
    pElementText,
    iTextLen,
    NULL,
    NULL );
    std::string strText;
    strText = pElementText;
    delete[] pElementText;
    return strText;
    }
    std::wstring ANSIToUnicode(std::string str ,UINT  CodePage)
    {
    wchar_t*     pElementText;
    int    iTextLen;
    // wide char to multi char
    iTextLen = ::MultiByteToWideChar( CodePage,
    0,
    str.c_str(),
    -1,
    NULL,
    NULL); pElementText = new wchar_t[iTextLen + 1];
    memset( ( void* )pElementText, 0, sizeof( wchar_t ) * ( iTextLen + 1 ) );
    ::MultiByteToWideChar( CodePage ,
    0,
    str.c_str(),
    -1,
    pElementText,
    iTextLen);
    std::wstring strText;
    strText = pElementText;
    delete[] pElementText;
    return strText;
    }
    我的项目中使用了 
    不会出现乱码