Some code I have laying around...
Code:
std::wstring utf8_to_wstr(const std::string &utf8)
{
// NOTE: we are assuming that the number of bytes in a UTF8 string is
// always >= to the number of wchar_t's required to represent that
// string in UTF16LE - which should hold true
std::vector<wchar_t> wbuff(utf8.length() + 1);
// NOTE: this does not NULL terminate the string in wbuff, but this is ok
// since it was zero-initialized in the vector constructor
if (!MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), utf8.length(),
&wbuff[0], utf8.length()))
{
return L"ErrorW";
}//if
return &wbuff[0];
}//utf8_to_wstr
std::string wstr_to_str(const std::wstring &wstr, UINT cp = CP_ACP)
{
int len = WideCharToMultiByte(cp, 0, wstr.c_str(), wstr.length(),
0, 0, 0, 0);
if (!len)
return "ErrorA";
std::vector<char> abuff(len + 1);
// NOTE: this does not NULL terminate the string in abuff, but this is ok
// since it was zero-initialized in the vector constructor
if (!WideCharToMultiByte(cp, 0, wstr.c_str(), wstr.length(),
&abuff[0], len, 0, 0))
{
return "ErrorA";
}//if
return &abuff[0];
}//wstr_to_str
gg