I am currently using the methods MultiByteToWideChar
and WideCharToMultiByte
of the Windows API to convert between std::string
and std::wstring
.
I am 'multiplatforming' my code removing Windows dependencies, so I would like to know alternative to the methods above. Concretely, using boost will be great. Which methods may I use? Here is the code I am currently using:
const std::wstring Use::stow(const std::string& str)
{
if (str.empty()) return L"";
int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0);
std::wstring wstrTo( size_needed, 0 );
MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed);
return wstrTo;
}
const std::string Use::wtos(const std::wstring& wstr)
{
if (wstr.empty()) return "";
int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);
std::string strTo( size_needed, 0 );
WideCharToMultiByte (CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL);
return strTo;
}