如何从 Windows 上的 C++ 控制台应用程序打印 UTF-8

2021-12-28 00:00:00 utf-8 c++ console-application

用于在英文 Windows(XP、Vista 或 7)上使用 Visual Studio 2008 编译的 C++ 控制台应用程序.是否可以打印到控制台并使用 cout 或 wcout 正确显示 UTF-8 编码的日语?

For a C++ console application compiled with Visual Studio 2008 on English Windows (XP,Vista or 7). Is it possible to print out to the console and correctly display UTF-8 encoded Japanese using cout or wcout?

推荐答案

这应该有效:

#include <cstdio>
#include <windows.h>

#pragma execution_character_set( "utf-8" )

int main()
{
    SetConsoleOutputCP( 65001 );
    printf( "Testing unicode -- English -- Ελληνικ? -- Espa?ol -- Русский. a?bcdefghijklmno?pqrs?tuüvwxyz
" );
}

不知道它是否会影响任何东西,但源文件保存为 Unicode(带签名的 UTF-8)-代码页 65001 在 FILE -> 高级保存选项....

Don't know if it affects anything, but source file is saved as Unicode (UTF-8 with signature) - Codepage 65001 at FILE -> Advanced Save Options ....

项目 -> 属性 -> 配置属性 -> 常规 -> 字符集 设置为使用 Unicode 字符集.

Project -> Properties -> Configuration Properties -> General -> Character Set is set to Use Unicode Character Set.

有人说您需要将控制台字体更改为 Lucida Console,但在我这边,它同时显示为 Consolas 和 Lucida Console.

Some say you need to change console font to Lucida Console, but on my side it is displayed with both Consolas and Lucida Console.

相关文章