如何编写统一code字符到控制台? [英] How to write Unicode characters to the console?

查看:161
本文介绍了如何编写统一code字符到控制台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用.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。 尝试使用这个(MSDN链接)发送前输出:

It's likely that your output encoding is set to ASCII. Try using this (MSDN link) before sending output:

Console.OutputEncoding = System.Text.Encoding.Unicode

和这里有一个小的控制台测试应用程序你会发现得心应手:

and here's a little console test app you may find handy:

C#

using System;
using System.Text;

public static class MyClass {
    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 MyModule
    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 Toolbar Menu (icon like C:.) and select Properties -> Font. Try some other fonts to see if they display your character properly:

这篇关于如何编写统一code字符到控制台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆