从字符串转换为wstring导致ú丢失编码 [英] Conversion from string to wstring is causing ú to lose encoding
本文介绍了从字符串转换为wstring导致ú丢失编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
变量 filepath
这是一个字符串
包含值Música
。我有以下代码:
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
然后包含值 M + SICA
。如何将 filepath
转换为 fp
而不会丢失ú字符的编码?
fp
then contains the value M?sica
. How do I convert filepath
to fp
without losing the encoding for the ú character?
推荐答案
使用功能MultiByteToWideChar。
Use the function MultiByteToWideChar.
示例代码:
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;
}
这篇关于从字符串转换为wstring导致ú丢失编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文