StringUtils.h 756 B

123456789101112131415161718192021
  1. #pragma once
  2. #include <string>
  3. #include <Windows.h>
  4. inline std::string wstringToUtf8(const std::wstring& src) {
  5. auto size = WideCharToMultiByte(CP_UTF8, 0, src.c_str(), static_cast<int>(src.size()), nullptr, 0, nullptr, nullptr);
  6. std::string result;
  7. result.resize(size, ' ');
  8. WideCharToMultiByte(CP_UTF8, 0, src.c_str(), static_cast<int>(src.size()), &result[0], size, nullptr, nullptr);
  9. return result;
  10. }
  11. inline std::wstring utf8ToWstring(const std::string& src) {
  12. auto size = MultiByteToWideChar(CP_UTF8, 0, src.c_str(), static_cast<int>(src.size()), nullptr, 0);
  13. std::wstring result;
  14. result.resize(size, ' ');
  15. MultiByteToWideChar(CP_UTF8, 0, src.c_str(), static_cast<int>(src.size()), &result[0], size);
  16. return result;
  17. }