C# 在控制台中显示 UTF-8 字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2062875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:09:38  来源:igfitidea点击:

Show UTF-8 characters in console

c#

提问by Adrya

How can I print UTF8 characters in the console?

如何在控制台中打印 UTF8 字符?

With Console.Writeline("???a?")I see ?asatin console.

随着Console.Writeline("???a?")?asat在控制台中看到。

采纳答案by Reed Copsey

There are some hacks you can find that demonstrate how to write multibyte character sets to the Console, but they are unreliable. They require your console font to be one that supports it, and in general, are something I would avoid. (All of these techniques break if your user doesn't do extra work on their part... so they are not reliable.)

您可以找到一些演示如何将多字节字符集写入控制台的技巧,但它们并不可靠。他们要求你的控制台字体支持它,一般来说,这是我会避免的。(如果您的用户不做额外的工作,所有这些技术都会失效……所以它们不可靠。)

If you need to write Unicode output, I highly recommend making a GUI application to handle this, instead of using the Console. It's fairly easy to make a simple GUI to just write your output to a control which supports Unicode.

如果您需要编写 Unicode 输出,我强烈建议您制作一个 GUI 应用程序来处理这个问题,而不是使用控制台。制作一个简单的 GUI 来将输出写入支持 Unicode 的控件是相当容易的。

回答by Guffa

You can't print Unicode characters in the console, it only supports the characters that are available in the current code page. Characters that are not available are converted to the closest equivalent, or a question mark.

您不能在控制台中打印 Unicode 字符,它仅支持当前代码页中可用的字符。不可用的字符将转换为最接近的等价字符或问号。

回答by Ozgur

Console.OutputEncoding = Encoding.UTF8;

回答by u4069409

Using Console.OutputEncoding will be sufficient for this. All string objects in .NET are by default unicode so changing output encoding for console to UTF-8 will work as you want in modern Windows installations.

为此,使用 Console.OutputEncoding 就足够了。.NET 中的所有字符串对象默认都是 unicode,因此将控制台的输出编码更改为 UTF-8 将在现代 Windows 安装中按您的需要工作。

Default encoding in console depends on configuration but it will be most likely IBM437 for US language or some local codepage.

控制台中的默认编码取决于配置,但对于美国语言或某些本地代码页很可能是 IBM437。

回答by jianchi wei

Try this :

尝试这个 :

using System.Diagnostics
...
Debug.WriteLine(..);// will output utf-8 charset

回答by Sunil Dhappadhule

The simple solution that I found below

我在下面找到的简单解决方案

Console.Write("???a?");