+#if defined(WIN32)
+ // Convert a wide Unicode string to an UTF8 string
+ char* utf8_encode(const wchar_t* encoded)
+ {
+ if (encoded == NULL) return NULL;
+ auto size_needed = WideCharToMultiByte(CP_UTF8, 0, encoded, (int)std::wcslen(encoded), NULL, 0, NULL, NULL);
+ char* strTo = new char[ size_needed + 1 ];
+ WideCharToMultiByte(CP_UTF8, 0, encoded, (int)std::wcslen(encoded), strTo, size_needed, NULL, NULL);
+ strTo[size_needed] = '\0';
+ return strTo;
+ }
+ // Convert an UTF8 string to a wide Unicode String
+ wchar_t* utf8_decode(const char* decoded)
+ {
+ if (decoded == NULL) return NULL;
+ auto size_needed = MultiByteToWideChar(CP_UTF8, 0, decoded, (int)strlen(decoded), NULL, 0);
+ wchar_t* wstrTo = new wchar_t[ size_needed + 1 ];
+ MultiByteToWideChar(CP_UTF8, 0, decoded, (int)strlen(decoded), wstrTo, size_needed);
+ wstrTo[size_needed] = '\0';
+ return wstrTo;
+ }
+ // Convert a wide Unicode std::wstring to an UTF8 string
+ std::string utf8_encode_s(const std::wstring& encoded) {
+ char* anEncoded = utf8_encode(encoded.c_str());
+ std::string res = std::string(anEncoded);
+ delete [] anEncoded;
+ return res;
+ }
+ // Convert an UTF8 std::string to a wide Unicode std::wstring
+ std::wstring utf8_decode_s(const std::string& decoded) {
+ wchar_t* aDecoded = utf8_decode(decoded.c_str());
+ std::wstring res = std::wstring(aDecoded);
+ delete [] aDecoded;
+ return res;
+ }
+#endif