Conversion from string to wstring is causing ú to lose encoding

情到浓时终转凉″ 提交于 2020-01-02 10:18:30

问题


The variable filepath which is a string contains the value Música. I have the following code:

wstring fp(filepath.length(), L' ');
copy(filepath.begin(), filepath.end(), fp.begin());

fp then contains the value M?sica. How do I convert filepath to fp without losing the encoding for the ú character?


回答1:


Use the function MultiByteToWideChar.

Sample code:

std::string toStdString(const std::wstring& s, UINT32 codePage)
{
    unsigned int bufferSize = (unsigned int)s.length()+1;
    char* pBuffer = new char[bufferSize];
    memset(pBuffer, 0, bufferSize);
    WideCharToMultiByte(codePage, 0, s.c_str(), (int)s.length(), pBuffer, bufferSize, NULL, NULL);
    std::string retVal = pBuffer;
    delete[] pBuffer;
    return retVal;
}

std::wstring toStdWString(const std::string& s, UINT32 codePage)
{
    unsigned int bufferSize = (unsigned int)s.length()+1;
    WCHAR* pBuffer = new WCHAR[bufferSize];
    memset(pBuffer, 0, bufferSize*sizeof(WCHAR));
    MultiByteToWideChar(codePage, 0, s.c_str(), (int)s.length(), pBuffer, bufferSize);
    std::wstring retVal = pBuffer;
    delete[] pBuffer;
    return retVal;
}



回答2:


Since you are using MFC, you have access to the ATL String Conversion Macros.

This greatly simplifies the conversion vs. using MultiByteToWideChar. Assuming that filepath is encoded in your system's default code page, this should do the trick:

CA2W wideFilepath(filepath.c_str());
wstring fp(static_cast<const wchar_t*>(wideFilepath));

If filepath is not in your system's default code page (let's say it's in UTF-8), then you can specify the encoding to convert from:

CA2W wideFilepath(filepath.c_str(), CP_UTF8);
wstring fp(static_cast<const wchar_t*>(wideFilepath));

To convert the other way, from std::wstring to std::string, you would do this:

// Convert from wide (UTF-16) to UTF-8
CW2A utf8Filepath(fp.c_str(), CP_UTF8);
string utf8Fp(static_cast<const char*>(utf8Filepath));

// Or, convert from wide (UTF-16) to your system's default code page.
CW2A narrowFilepath(fp.c_str(), CP_UTF8);
string narrowFp(static_cast<const char*>(narrowFilepath));


来源:https://stackoverflow.com/questions/2400383/conversion-from-string-to-wstring-is-causing-%c3%ba-to-lose-encoding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!