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:
Here's a revised code snippet that incorporates these solutions:
#include
#include
#include
#include
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 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.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3