通过 C++ 程序在 PowerShell 中打印 unicode 字符 [英] Printing unicode characters in PowerShell via a C++ program
问题描述
我的最终目标是通过 C++ 程序将一些非拉丁文本输出写入 Windows 中的控制台.
My end goal here is to write some non-latin text output to console in Windows via a C++ program.
cmd.exe 无处可去,所以我得到了最新的闪亮版本的 PowerShell(支持 unicode).我已经确认我可以
cmd.exe gets me nowhere, so I got the latest, shiny version of PowerShell (that supports unicode). I've verified that I can
- 输入非 Unicode 字符和
- 查看来自 Windows 命令的非 unicode 控制台输出(如dir")
例如,我有这个文件,京语.txt"(京语是韩语字母表中的第一个字母),我可以得到这样的输出:
for example, I have this file, "가.txt" (가 is the first letter in the korean alphabet) and I can get an output like this:
PS P:\reference\unicode> dir .\가.txt
Directory: P:\reference\unicode
Mode LastWriteTime Length
Name
---- ------------- ------
----
-a--- 1/12/2010 8:54 AM 0 가.txt
到目前为止一切顺利.但是使用 C++ 程序写入控制台不起作用.
So far so good. But writing to console using a C++ program doesn't work.
int main()
{
wchar_t text[] = {0xAC00, 0}; // 가 has code point U+AC00 in unicode
wprintf(L"%s", text); // this prints a single question mark: "?"
}
我不知道我错过了什么.我可以输入并在控制台上看到 사 的事实似乎表明我有三个需要的部分(unicode 支持、字体和字形),但我一定是弄错了.
I don't know what I'm missing. The fact that I can type-in and see 가 on the console seems to indicate that I have the three needed pieces (unicode support, font and glyph), but I must be mistaken.
我也试过chcp",但没有任何运气.我的 C++ 程序做错了吗?
I've also tried "chcp" without any luck. Am I doing something wrong in my C++ program?
谢谢!
推荐答案
来自 printf 文档:
From the printf docs:
wprintf 和 printf 的行为相同如果流以 ANSI 模式打开.
wprintf and printf behave identically if the stream is opened in ANSI mode.
查看此博文.它有这个漂亮的简短列表:
Check out this blog post. It has this nice short little listing:
#include <fcntl.h>
#include <io.h>
#include <stdio.h>
int main(void) {
_setmode(_fileno(stdout), _O_U16TEXT);
wprintf(L"\x043a\x043e\x0448\x043a\x0430 \x65e5\x672c\x56fd\n");
return 0;
}
这篇关于通过 C++ 程序在 PowerShell 中打印 unicode 字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!