LogIt++
Loading...
Searching...
No Matches
encoding_utils.hpp
Go to the documentation of this file.
1#ifndef _LOGIT_ENCODING_UTILS_HPP_INCLUDED
2#define _LOGIT_ENCODING_UTILS_HPP_INCLUDED
3
6
7#if defined(_WIN32)
8#include <windows.h>
9
10namespace logit {
11
15 std::string utf8_to_ansi(const std::string& utf8) {
16 int n_len = MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, NULL, 0);
17 if (n_len == 0) return {};
18
19 std::wstring wide_string(n_len + 1, L'\0');
20 MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, &wide_string[0], n_len);
21
22 n_len = WideCharToMultiByte(CP_ACP, 0, wide_string.c_str(), -1, NULL, 0, NULL, NULL);
23 if (n_len == 0) return {};
24
25 std::string ansi_string(n_len - 1, '\0');
26 WideCharToMultiByte(CP_ACP, 0, wide_string.c_str(), -1, &ansi_string[0], n_len, NULL, NULL);
27 return ansi_string;
28 }
29
30};
31#endif
32
33#endif // _LOGIT_ENCODING_UTILS_HPP_INCLUDED
The primary namespace for the LogIt++ library.