Conversion from string to wstring is causing ú to

2019-05-18 09:16发布

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?

2条回答
\"骚年 ilove
2楼-- · 2019-05-18 09:25

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));
查看更多
干净又极端
3楼-- · 2019-05-18 09:46

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;
}
查看更多
登录 后发表回答