问题描述
我想知道是否可以在控制台应用程序中使用 .NET 编写像 ℃
这样的字符.当我尝试写这个字符时,控制台会输出一个问号.
I was wondering if it was possible, in a console application, to write characters like ℃
using .NET. When I try to write this character, the console outputs a question mark.
推荐答案
很可能您的输出编码设置为 ASCII.在发送输出之前尝试使用它:
It's likely that your output encoding is set to ASCII. Try using this before sending output:
Console.OutputEncoding = System.Text.Encoding.UTF8;
(MSDN 链接 到支持文件.)
(MSDN link to supporting documentation.)
这里有一个小控制台测试应用程序,您可能会觉得很方便:
And here's a little console test app you may find handy:
C#
using System;
using System.Text;
public static class ConsoleOutputTest {
public static void Main() {
Console.OutputEncoding = System.Text.Encoding.UTF8;
for (var i = 0; i <= 1000; i++) {
Console.Write(Strings.ChrW(i));
if (i % 50 == 0) { // break every 50 chars
Console.WriteLine();
}
}
Console.ReadKey();
}
}
VB.NET
imports Microsoft.VisualBasic
imports System
public module ConsoleOutputTest
Sub Main()
Console.OutputEncoding = System.Text.Encoding.UTF8
dim i as integer
for i = 0 to 1000
Console.Write(ChrW(i))
if i mod 50 = 0 'break every 50 chars
Console.WriteLine()
end if
next
Console.ReadKey()
End Sub
end module
您选择的控制台字体也可能不支持该特定字符.单击 Windows 工具栏菜单(类似 C:. 的图标)并选择属性 -> 字体.尝试一些其他字体,看看它们是否能正确显示您的角色:
It's also possible that your choice of Console font does not support that particular character. Click on the Windows Tool-bar Menu (icon like C:.) and select Properties -> Font. Try some other fonts to see if they display your character properly:
这篇关于如何将 Unicode 字符写入控制台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!