Home  >  Article  >  Backend Development  >  How to Output UTF-8 Strings Correctly to `std::cout` on Windows?

How to Output UTF-8 Strings Correctly to `std::cout` on Windows?

Susan Sarandon
Susan SarandonOriginal
2024-10-31 07:36:01341browse

How to Output UTF-8 Strings Correctly to `std::cout` on Windows?

Dealing with UTF-8 Strings in C on Windows

Encoding strings as UTF-8 is widely used for cross-platform applications. However, outputting UTF-8 strings to std::cout on Windows presents unique challenges.

The default behavior on Windows is for std::cout to expect strings in non-Unicode formats. When presented with UTF-8 strings, it displays corrupted characters.

To address this issue, there are two primary steps:

  1. Set Console Code Page to UTF-8: Using the SetConsoleOutputCP function, inform the console that the incoming byte stream is UTF-8 encoded.
  2. Enable Stream Buffering: Disable the default behavior of std::basic_filebuf in Visual Studio, which breaks down UTF-8 byte sequences and passes them as individual bytes. To overcome this, setvbuf enables stream buffering, ensuring the entire string is passed through as a whole.

Here's a revised code snippet that incorporates these solutions:

<code class="cpp">#include <string>
#include <iostream>
#include <Windows.h>
#include <cstdio>

int main() {
    // Set console code page to UTF-8
    SetConsoleOutputCP(CP_UTF8);

    // Enable buffering to prevent byte-by-byte transmission
    setvbuf(stdout, nullptr, _IOFBF, 1000);

    // Output UTF-8 string
    std::string test = u8"Greek: αβγδ; German: Übergrößenträger";
    std::cout << test << std::endl;

    return 0;
}</code>

In addition to these steps, note that raster fonts in the Windows console may not display non-ASCII Unicode characters correctly. To enable proper rendering, it's recommended to switch to a TrueType font, which is now the default in Windows 10 and later versions.

The above is the detailed content of How to Output UTF-8 Strings Correctly to `std::cout` on Windows?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn