Console.OutputEncoding相应设置,但控制台仍然打印geeberish [英] Console.OutputEncoding set accordingly but Console still prints geeberish

查看:137
本文介绍了Console.OutputEncoding相应设置,但控制台仍然打印geeberish的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在调试中我确实看到了正确的字符。为什么Visual Studio和System.Console编码有所不同?如何使控制台的编码符合VS的一个?



有几个帖子询问类似的问题,但我们缺少一个简单的答案。我在下面列出所有我尝试的替代方案 - 哪些不起作用。



有些帖子提到,为了控制台正确打印它们,您需要具有控制台输出编码正确设置+有一个支持该问题的字符集的字体。我只是找不到答案(如果可能的话)更改控制台的字体。



1 。设置控制台.OutputEncoding 到UTF8或

  Encoding.GetEncoding(1255); 

在我的情况下,我正在处理希伯来文字符。



2 。尝试打印

  Encoding.GetEncoding(1255).GetString的结果(Encoding.Default.GetBytes(myString))

再次,在调试中,字符串确实显示。是的,我对字符编码的主题不太熟悉,我希望这不是简单的东西,通过System.Console显示非ASCII字符。

解决方案

以下是答案,最后:


为什么Visual Studio和系统.Console编码有所不同?如何使
控制台的编码与VS的匹配?


答案
将System.Console.OutputEncoding 设置为UTF8,Unicode或任何您需要做的任务。如果字符仍然显示不正确,则问题出在字体窗口命令提示符(我们称之为VS中的控制台)被配置为不支持您尝试显示的字符集。简而言之,那个问题就是字体


我找不到答案可以)更改
控制台的字体。


答案


In debug I do see the characters properly. Why do Visual Studio and System.Console encodings differ ? How to make console's encoding match VS's one ?

There are a couple of posts inquiring about a similar problem, but we are lacking a straightforward answer. I am listing below all the alternatives I tried - which did not work.

Some posts mention that in order to the console print them correctly, you need to have the Console output encoding properly set + have a font that supports the character set in question. I just could not find an answer (if even possible) to change the console's font.

1.Setting Console.OutputEncoding to UTF8 or to

Encoding.GetEncoding(1255);

In my case I am dealing with hebrew characters.

2.Tried printing the result of

Encoding.GetEncoding(1255).GetString(Encoding.Default.GetBytes(myString))

Again, in debug the strings do display correctly. And yes, I have not much familiarity with the topic of character encoding, and I am hoping this is not required for something as simple as this, displaying non-ascii characters via System.Console.

解决方案

Here are the answers, finally:

Why do Visual Studio and System.Console encodings differ ? How to make console's encoding match VS's one ?

Answer: Setting System.Console.OutputEncoding to UTF8, Unicode or whatever else you need does the job. If the characters still keep being displayed incorrectly, the problem is with the font windows command prompt (what in VS we call Console) is configured to use not supporting the character set you are trying to display. In a short, the problem is then the font.

I just could not find an answer (if even possible) to change the console's font.

Answer: Here's a great link that will walk you through accomplishing such task.

After adding additional fonts for display in the command prompt, this answer shows screenshots of how do select a newly added font.

In a short, if you ended up in this post and are trying to have some non-ascii character set being printed in a command prompt window, the solution is setting System.Console.OuputEncoding accordingly + making sure the font configured to be used by your command prompt supports the character set you are dealing with.

In addition - if you need the Console to support Right-To-Left printing, having found no built-in support, you can manually play with the cursor position, something like:

private static void Main(string[] args)
{
    Console.OutputEncoding = Encoding.Unicode;

    List<string> sentences = new List<string>();

    sentences.Add(string.Format("אברהם"));
    sentences.Add(string.Format("שורה {0}, אחרי אברהם", 2));

    foreach (var sentence in sentences)
    {
        WriteLineRTL(sentence);
    }
}

private static void WriteLineRTL(string input)
{
    var chars = input.ToCharArray();
    Console.CursorLeft = chars.Length;
    for (int i = 0; i < chars.Length; i++)
    {
        Console.Write(chars[i]);
        Console.CursorLeft -= 2;
    }
    Console.WriteLine();
}

Output:

这篇关于Console.OutputEncoding相应设置,但控制台仍然打印geeberish的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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