2

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.

Veverke
  • 9,208
  • 4
  • 51
  • 95
  • Console doesn't print correctly because of font. – Hamlet Hakobyan Sep 20 '15 at 12:26
  • @HamletHakobyan: Hamlet, I forgot to mention about fonts in my post, updated it - in a short, if the font is the solution, how does one change the console's one - I could not find such answer. – Veverke Sep 20 '15 at 12:31
  • You haven't so much choices for fonts. If your OS language is English I think there is no font for console with Hebrew character set defined. Maybe it could be possible to solve issue if you install Hebrew language pack for OS. – Hamlet Hakobyan Sep 20 '15 at 12:44
  • @HamletHakobyan: my windows supports Hebrew everywhere else, like in text-editors, change language-input... I think this means I already have the Hebrew language package installed. – Veverke Sep 20 '15 at 12:51
  • Have you tried all fonts available in Font tab in the property dialog of your console? – Hamlet Hakobyan Sep 20 '15 at 13:46
  • @HamletHakobyan: do you know where in VS options I do that ? – Veverke Sep 20 '15 at 14:05
  • At first you should check if there is font supporting Hebrew charset as I've mentioned above. – Hamlet Hakobyan Sep 20 '15 at 14:32
  • Related to http://stackoverflow.com/questions/5750203/how-to-write-unicode-chars-to-console ? – Alastair McCormack Sep 20 '15 at 21:15
  • 1
    Did you set the console font as well, as described in my link? – Alastair McCormack Sep 21 '15 at 08:38
  • @AlastairMcCormack: I do not see how this helps, it suggests changing Console.OutputEncoding as mentioned in the attempts I tried above as not having worked. However, the post you bring to my attention does show how to change console fonts, something I was not being able to do till now (I was thinking it had to be done via VS settings.) Now the problem is: how to add fonts to the console, because those 3 it offer apparently does not support Hebrew. Am following [**this**](http://www.codeproject.com/Articles/30040/Font-Survey-42-of-the-Best-Monospaced-Programming) to do that. – Veverke Sep 21 '15 at 09:48
  • @AlastairMcCormack: That's it. (I replaced the link to the article that shows how to add new display fonts to the command prompt, because the previous was not as complete as the new one). Fixed. So the solution is comprised of changing the Console's output encoding **AND** making sure the font your command prompt is using to print supports the character set you are dealing with. Please post this an answer so I can give you the points. Just make sure the answer is complete, because there are several posts on this topic so I would like to help giving a definite and concise solution. Thanks. – Veverke Sep 21 '15 at 09:51
  • Hi @Veverke, that's really kind. I'm only aware of Console problems due to working with Python on the console. I think the best thing to do is to mark this question a duplicate of the question I linked or for you to answer it yourself so you can give a proper .net perspective :) – Alastair McCormack Sep 21 '15 at 10:18
  • @AlastairMcCormack: as you wish. It's really not a question of being kind or not, it's of principle: knowledge is not something one owns, it is something one acquires. You led me to missing part of my puzzle, so I owe you that. I just want to record the whole procedure (questions + answers to all of what was asked) so other can find the solution for the problem when stumbling across it. – Veverke Sep 21 '15 at 10:27
  • 1
    @AlastairMcCormack: I edited the answer from the post you mention adding the link that solved my problem, is est, guided me on how to add additional display fonts to the command prompt. I would rather keep this post just because I think (obviously biased by the fact that I wrote it) it is more concise and clearer to reach the solution. – Veverke Sep 21 '15 at 10:32

1 Answers1

3

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:

enter image description here

Community
  • 1
  • 1
Veverke
  • 9,208
  • 4
  • 51
  • 95