From 6192d312aec4e3f3b910d0e7759758c3bf6925f9 Mon Sep 17 00:00:00 2001 From: taynpg Date: Mon, 20 Jan 2025 16:36:54 +0800 Subject: [PATCH] =?UTF-8?q?is=EF=BC=9A=E7=A4=BA=E4=BE=8B=E6=9D=A5=E6=BA=90?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 demo.cpp diff --git a/demo.cpp b/demo.cpp new file mode 100644 index 0000000..24b7f55 --- /dev/null +++ b/demo.cpp @@ -0,0 +1,47 @@ +#include +//#include //_setmode() +//#include // +#include +#include "windows.h" + +// source link: https://github.com/microsoft/terminal/issues/15380 + +int main() +{ + using namespace std; + auto out_cp = GetConsoleOutputCP(); // To restore output code page at exit. + auto inp_cp = GetConsoleCP(); // To restore input code page at exit. + SetConsoleOutputCP(CP_UTF8); // Set console output code page to UTF-8 encoding. + SetConsoleCP(CP_UTF8); // Set console input code page to UTF-8 encoding. + + cout << "Test: γ‚γ‚γ‚πŸ™‚πŸ™‚πŸ™‚ζ—₯ζœ¬πŸ‘ŒδΈ­ζ–‡πŸ‘ΠšΠΈΡ€ΠΈΠ»Π»ΠΈΡ†Π°" << endl; // Make sure you save your project file with 65001(UTF-8) encoding. + + // Update: Windows console UTF-8 input has been fixed in #14745. + auto utf8 = string{}; + cout << "Enter text: "; + cin >> utf8; + cout << "UTF-8 text: " << utf8 << endl; + + // Outdated. + //auto wide = wstring{}; + //auto utf8 = string{}; + //cout << "Enter text: "; + //// stdin should be configured in order to receive wchar_t (you can't receive in UTF-8 encoding on Windows yet) + //_setmode(_fileno(stdin), _O_U16TEXT); + //wcin >> wide; + // + //// Optional: stdout should be configured to output wide strings + //_setmode(_fileno(stdout), _O_U16TEXT); + //wcout << L"Wide text: " << wide << endl; + //_setmode(_fileno(stdout), _O_TEXT); // Restore to UTF-8. + // + //// or convert wide-string to UTF-8 string before output it + //utf8.resize(wide.size() * 3); // Resize utf8 buffer for the worst case. + //auto size = WideCharToMultiByte(CP_UTF8, 0, wide.data(), (DWORD)wide.size(), utf8.data(), (DWORD)utf8.size(), 0, 0); + //utf8.resize(size); + //cout << "UTF-8 text: " << utf8 << endl; + + SetConsoleOutputCP(out_cp); // Restore original system code pages. + SetConsoleCP(inp_cp); // + return 0; +}