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:
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.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:
Output: