开发者

Conversion from string to wstring is causing ú to lose encoding

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

wstring 开发者_Go百科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?


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


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));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜