-
Notifications
You must be signed in to change notification settings - Fork 0
/
utf8_to_multibyte_dynamic.cpp
35 lines (26 loc) · 1.06 KB
/
utf8_to_multibyte_dynamic.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <windows.h> // console functions
#include <cstdio> // std::printf, std::size_t
#include <string> // std::string, std::wstring
#include <memory> // std::unique_ptr, std::make_unique
#define DEFAULT_INPUT_LENGTH 255 // any length > 0 really
int main()
{
SetConsoleCP(CP_UTF8);
SetConsoleOutputCP(CP_UTF8);
WCHAR w_str[DEFAULT_INPUT_LENGTH];
DWORD n_read;
HANDLE console = GetStdHandle(STD_INPUT_HANDLE);
std::wstring ws;
while (ReadConsoleW(console, w_str, DEFAULT_INPUT_LENGTH, &n_read, NULL))
{
ws += std::wstring(w_str, static_cast<std::size_t>(n_read));
if (ws.find_last_of(L"\r\n") != std::wstring::npos)
break;
}
int mb_size = WideCharToMultiByte(CP_UTF8, 0, ws.data(), ws.length(), NULL, 0, NULL, NULL);
std::unique_ptr<char[]> mb_str = std::make_unique<char[]>(static_cast<std::size_t>(mb_size));
WideCharToMultiByte(CP_UTF8, 0, ws.data(), ws.length(), mb_str.get(), mb_size, NULL, NULL);
std::string s(mb_str.get(), static_cast<std::size_t>(mb_size));
std::printf("ENTERED: %s\n", s.c_str()); // because std::cout prints gibberish
return 0;
}