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
5
6#if defined(_WIN32)
7#include <windows.h>
8
9namespace logit {
10
14 std::string utf8_to_ansi(const std::string& utf8) {
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
29};
30#endif
31
32#endif // _LOGIT_ENCODING_UTILS_HPP_INCLUDED
The primary namespace for the LogIt++ library.
std::string utf8_to_ansi(const std::string &utf8)
Converts a UTF-8 string to an ANSI string (Windows-specific).