Guest User

cpToWstr, wstrToUtf8

a guest
Aug 27th, 2015
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.39 KB | None | 0 0
  1. std::wstring Language::cpToWstr(const std::string& src)
  2. {
  3.     if (src.empty())
  4.         return L"";
  5. #ifdef _WIN32
  6.     int size = MultiByteToWideChar(CP_ACP, 0, &src[0], (int)src.size(), NULL, 0);
  7.     std::wstring wstr(size, 0);
  8.     MultiByteToWideChar(CP_ACP, 0, &src[0], (int)src.size(), &wstr[0], size);
  9.     return wstr;
  10. #else
  11.     const int MAX = 500;
  12.     wchar_t buffer[MAX + 1];
  13.     setlocale(LC_ALL, "");
  14.     size_t len = mbstowcs(buffer, src.c_str(), MAX);
  15.     setlocale(LC_ALL, "C");
  16.     if (len == (size_t)-1)
  17.         return L"?";
  18.     return std::wstring(buffer, len);
  19. #endif
  20. }
  21.  
  22. std::string Language::wstrToUtf8(const std::wstring& src)
  23. {
  24.     if (src.empty())
  25.         return "";
  26. #ifdef _WIN32
  27.     int size = WideCharToMultiByte(CP_UTF8, 0, &src[0], (int)src.size(), NULL, 0, NULL, NULL);
  28.     std::string str(size, 0);
  29.     WideCharToMultiByte(CP_UTF8, 0, &src[0], (int)src.size(), &str[0], size, NULL, NULL);
  30.     return str;
  31. #else
  32.     std::string out;
  33.     unsigned int codepoint = 0;
  34.     for (std::wstring::const_iterator i = src.begin(); i != src.end(); ++i)
  35.     {
  36.         wchar_t ch = *i;
  37.         if (ch >= 0xd800 && ch <= 0xdbff)
  38.             codepoint = ((ch - 0xd800) << 10) + 0x10000;
  39.         else
  40.         {
  41.             if (ch >= 0xdc00 && ch <= 0xdfff)
  42.                 codepoint |= ch - 0xdc00;
  43.             else
  44.                 codepoint = ch;
  45.  
  46.             if (codepoint <= 0x7f)
  47.                 out.append(1, static_cast<char>(codepoint));
  48.             else if (codepoint <= 0x7ff)
  49.             {
  50.                 out.append(1, static_cast<char>(0xc0 | ((codepoint >> 6) & 0x1f)));
  51.                 out.append(1, static_cast<char>(0x80 | (codepoint & 0x3f)));
  52.             }
  53.             else if (codepoint <= 0xffff)
  54.             {
  55.                 out.append(1, static_cast<char>(0xe0 | ((codepoint >> 12) & 0x0f)));
  56.                 out.append(1, static_cast<char>(0x80 | ((codepoint >> 6) & 0x3f)));
  57.                 out.append(1, static_cast<char>(0x80 | (codepoint & 0x3f)));
  58.             }
  59.             else
  60.             {
  61.                 out.append(1, static_cast<char>(0xf0 | ((codepoint >> 18) & 0x07)));
  62.                 out.append(1, static_cast<char>(0x80 | ((codepoint >> 12) & 0x3f)));
  63.                 out.append(1, static_cast<char>(0x80 | ((codepoint >> 6) & 0x3f)));
  64.                 out.append(1, static_cast<char>(0x80 | (codepoint & 0x3f)));
  65.             }
  66.             codepoint = 0;
  67.         }
  68.     }
  69.     return out;
  70. #endif
  71. }
Advertisement
Add Comment
Please, Sign In to add comment