Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5. std::cout << u8"это строка6" << std::endl;
  6. return 0;
  7. }
  8.  
  9. ��то строка6
  10.  
  11. #include <iostream>
  12. #include <windows.h>
  13. #include <fcntl.h>
  14. #include <io.h>
  15. #include <iostream> // std::wcerr
  16.  
  17. std::wstring strtows(const std::string &str, UINT codePage)
  18. {
  19. std::wstring ws;
  20. int n = MultiByteToWideChar(codePage, 0, str.c_str(), static_cast<int>(str.size()), NULL, 0);
  21. if (n) {
  22. ws.resize(n);
  23. if (MultiByteToWideChar(codePage, 0, str.c_str(), static_cast<int>(str.size()), &ws[0], n) == 0)
  24. ws.clear();
  25. }
  26. return ws;
  27. }
  28.  
  29. std::string wstostr(const std::wstring &ws, UINT codePage)
  30. {
  31. // prior to C++11 std::string and std::wstring were not guaranteed to have their memory be contiguous,
  32. // although all real-world implementations make them contiguous
  33. std::string str;
  34. int srcLen = static_cast<int>(ws.size());
  35. int n = WideCharToMultiByte(codePage, 0, ws.c_str(), srcLen, NULL, 0, 0, NULL);
  36. if (n) {
  37. str.resize(n);
  38. if (WideCharToMultiByte(codePage, 0, ws.c_str(), srcLen, &str[0], n, 0, NULL) == 0)
  39. str.clear();
  40. }
  41. return str;
  42. }
  43.  
  44. std::string WstringToUtf8(const std::wstring &str)
  45. {
  46. return wstostr(str, CP_UTF8);
  47. }
  48.  
  49. std::wstring Utf8ToWstring(const std::string &str)
  50. {
  51.  
  52. return strtows(str, CP_UTF8);
  53. }
  54.  
  55. int main(int argc, char *argv[])
  56. {
  57. _setmode(_fileno(stdout), _O_U16TEXT);
  58. std::wcout << Utf8ToWstring(u8"это строка6") << std::endl;
  59. return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement