If you used C ++ 11, this would be easy:
std::string msg = u8"महसुस";
But since you will not do this, you can use escape sequences and not rely on the encoding of the source file to control the encoding for you, this way your code will be more portable (if you accidentally save it in a format other than UTF8)
std::string msg = "\xE0\xA4\xAE\xE0\xA4\xB9\xE0\xA4\xB8\xE0\xA5\x81\xE0\xA4\xB8";
Otherwise, you can instead perform the conversion at runtime:
std::string toUtf8(const std::wstring &str) { std::string ret; int len = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), str.length(), NULL, 0, NULL, NULL); if (len > 0) { ret.resize(len); WideCharToMultiByte(CP_UTF8, 0, str.c_str(), str.length(), &ret[0], len, NULL, NULL); } return ret; }
std::string msg = toUtf8(L"महसुस");
Remy Lebeau
source share