Kurlyk
Loading...
Searching...
No Matches
encoding_utils.hpp
Go to the documentation of this file.
1#pragma once
2#ifndef _KURLYK_UTILS_ENCODING_UTILS_HPP_INCLUDED
3#define _KURLYK_UTILS_ENCODING_UTILS_HPP_INCLUDED
4
7
8namespace kurlyk::utils {
9
10# if defined(_WIN32)
14 std::string utf8_to_ansi(const std::string& utf8) noexcept {
15 int n_len = MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, NULL, 0);
16 if (n_len == 0) return {};
17
18 std::wstring wide_string(n_len + 1, L'\0');
19 MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, &wide_string[0], n_len);
20
21 n_len = WideCharToMultiByte(CP_ACP, 0, wide_string.c_str(), -1, NULL, 0, NULL, NULL);
22 if (n_len == 0) return {};
23
24 std::string ansi_string(n_len - 1, '\0');
25 WideCharToMultiByte(CP_ACP, 0, wide_string.c_str(), -1, &ansi_string[0], n_len, NULL, NULL);
26 return ansi_string;
27 }
28# endif
29
30} // namespace kurlyk::utils
31
32#endif // _KURLYK_UTILS_ENCODING_UTILS_HPP_INCLUDED