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

问题描述 投票:0回答:9

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

c++ utf-8 console-application
9个回答
16
投票

这应该有效:

#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\n" );
}

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

Project -> Properties -> Configuration Properties -> General -> Character Set 设置为 Use Unicode Character Set.

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


10
投票

Windows 控制台默认使用 OEM 代码页 来显示输出。

要将代码页更改为 Unicode,请在控制台中输入

chcp 65001
,或尝试使用
SetConsoleOutputCP
以编程方式更改代码页。

请注意,您可能必须将控制台的字体更改为具有 unicode 范围内字形的字体。


7
投票

这里有一篇来自 MVP Michael Kaplan 的文章,介绍如何通过控制台正确输出 UTF-16。您可以将 UTF-8 转换为 UTF-16 并输出。


4
投票

我从来没有真正尝试过将控制台代码页设置为 UTF8(不知道为什么它不起作用......控制台可以很好地处理其他多字节代码页),但是有几个函数可以查找:SetConsoleCP 和 SetConsoleOutputCP。

您可能还需要确保您使用的是能够显示字符的控制台字体。有SetCurrentConsoleFontEx功能,但它只适用于Vista及以上版本。

希望有帮助。


2
投票

在应用程序启动控制台上设置为默认 OEM437 CP。 我试图将 Unicode 文本输出到标准输出,其中控制台切换到 UTF8 翻译 _setmode(_fileno(stdout), _O_U8TEXT);即使使用 Lucida TT 字体,屏幕上仍然没有运气。 如果控制台被重定向到文件,则会创建正确的 UTF8 文件。

最后我很幸运。我添加了单行“info.FontFamily = FF_DONTCARE;”它现在正在工作。 希望这对你有帮助。

void SetLucidaFont()
{
    HANDLE StdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_FONT_INFOEX info;
    memset(&info, 0, sizeof(CONSOLE_FONT_INFOEX));
    info.cbSize = sizeof(CONSOLE_FONT_INFOEX);              // prevents err=87 below
    if (GetCurrentConsoleFontEx(StdOut, FALSE, &info))
    {
        info.FontFamily   = FF_DONTCARE;
        info.dwFontSize.X = 0;  // leave X as zero
        info.dwFontSize.Y = 14;
        info.FontWeight   = 400;
        _tcscpy_s(info.FaceName, L"Lucida Console");
        if (SetCurrentConsoleFontEx(StdOut, FALSE, &info))
        {
        }
    }
}

1
投票

仅供补充信息:

“ANSI”是指 windows-125x,用于 win32 应用程序,而“OEM”是指控制台/MS-DOS 应用程序使用的代码页。
可以使用函数 GetOEMCP() 和 GetACP() 检索当前活动的代码页。

为了向控制台正确输出一些东西,你应该:

  1. 确保当前 OEM 代码页支持您要输出的字符
    (如有必要,使用SetConsoleOutputCP适当设置)

  2. 将字符串从当前 ANSI 代码 (win32) 转换为控制台 OEM 代码页

这里有一些实用程序:

// Convert a UTF-16 string (16-bit) to an OEM string (8-bit) 
#define UNICODEtoOEM(str)   WCHARtoCHAR(str, CP_OEMCP)

// Convert an OEM string (8-bit) to a UTF-16 string (16-bit) 
#define OEMtoUNICODE(str)   CHARtoWCHAR(str, CP_OEMCP)

// Convert an ANSI string (8-bit) to a UTF-16 string (16-bit) 
#define ANSItoUNICODE(str)  CHARtoWCHAR(str, CP_ACP)

// Convert a UTF-16 string (16-bit) to an ANSI string (8-bit)
#define UNICODEtoANSI(str)  WCHARtoCHAR(str, CP_ACP)


/* Convert a single/multi-byte string to a UTF-16 string (16-bit).
 We take advantage of the MultiByteToWideChar function that allows to specify the charset of the input string.
*/
LPWSTR CHARtoWCHAR(LPSTR str, UINT codePage) {
    size_t len = strlen(str) + 1;
    int size_needed = MultiByteToWideChar(codePage, 0, str, len, NULL, 0);
    LPWSTR wstr = (LPWSTR) LocalAlloc(LPTR, sizeof(WCHAR) * size_needed);
    MultiByteToWideChar(codePage, 0, str, len, wstr, size_needed);
    return wstr;
}


/* Convert a UTF-16 string (16-bit) to a single/multi-byte string.
 We take advantage of the WideCharToMultiByte function that allows to specify the charset of the output string.
*/
LPSTR WCHARtoCHAR(LPWSTR wstr, UINT codePage) {
    size_t len = wcslen(wstr) + 1;
    int size_needed = WideCharToMultiByte(codePage, 0, wstr, len, NULL, 0, NULL, NULL);
    LPSTR str = (LPSTR) LocalAlloc(LPTR, sizeof(CHAR) * size_needed );
    WideCharToMultiByte(codePage, 0, wstr, len, str, size_needed, NULL, NULL);
    return str;
}

1
投票

任何需要从文件中读取 UTF-8 并打印到控制台的人都可以尝试

wifstream
,即使在 visual studio 调试器中也能正确显示 UTF-8 字词(我正在处理繁体中文),来自这篇文章

#include <sstream>
#include <fstream>
#include <codecvt>

std::wstring readFile(const char* filename)
{
    std::wifstream wif(filename);
    wif.imbue(std::locale(std::locale::empty(), new std::codecvt_utf8<wchar_t>));
    std::wstringstream wss;
    wss << wif.rdbuf();
    return wss.str();
}
 
//  usage
std::wstring wstr2;
wstr2 = readFile("C:\\yourUtf8File.txt");
wcout << wstr2;

0
投票

在控制台中,输入

chcp 65001
将代码页更改为UTF-8的代码页。


0
投票

您可以使用

system
电话:

#include <stdlib.h>
#include <stdio.h>

int main() {
    system("chcp 65001");
    printf("%s\n", "中文");
}
© www.soinside.com 2019 - 2024. All rights reserved.