41

How can I print UTF8 characters in the console?

With Console.Writeline("îăşâţ") I see îasât in console.

SandPiper
  • 2,816
  • 5
  • 30
  • 52
Adrya
  • 3,427
  • 8
  • 31
  • 29

5 Answers5

89
Console.OutputEncoding = Encoding.UTF8;
Ozgur
  • 907
  • 1
  • 6
  • 2
11

There are some hacks you can find that demonstrate how to write multibyte character sets to the Console, but they are unreliable. They require your console font to be one that supports it, and in general, are something I would avoid. (All of these techniques break if your user doesn't do extra work on their part... so they are not reliable.)

If you need to write Unicode output, I highly recommend making a GUI application to handle this, instead of using the Console. It's fairly easy to make a simple GUI to just write your output to a control which supports Unicode.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • 22
    I'm going to assume `Console.OutputEncoding` did not exist in early 2010, but it would be nice to mention in the accepted answer. – 3ventic Nov 01 '14 at 04:19
7

Try this :

using System.Diagnostics
...
Debug.WriteLine(..);// will output utf-8 charset
DINA TAKLIT
  • 7,074
  • 10
  • 69
  • 74
jianchi wei
  • 71
  • 1
  • 1
  • Writes it to where, exactly? Not my console. https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.debug.writeline – Maxim Paperno Feb 24 '22 at 13:47
  • 1
    @MaximPaperno It writes to the Visual Studio Debugger console. Which is fortunate, because the top-rated answer does not work in that context _(it throws the "handle is invalid" error mentioned in the comments)_ – BlueRaja - Danny Pflughoeft Feb 01 '23 at 06:34
5

Using Console.OutputEncoding will be sufficient for this. All string objects in .NET are by default unicode so changing output encoding for console to UTF-8 will work as you want in modern Windows installations.

Default encoding in console depends on configuration but it will be most likely IBM437 for US language or some local codepage.

  • 1
    I tried setting the `Console.OutputEncoding = Encoding.Unicode` but now non western characters are displayed as little rectangles. Interestingly, if I mark the text in the console and paste it here I get the original unicode (e.g. `汤姆·福特`). PS: I'm using Windows Server 2016 my region is set to United Kingdom. – bounav Aug 23 '18 at 10:39
3

You can't print Unicode characters in the console, it only supports the characters that are available in the current code page. Characters that are not available are converted to the closest equivalent, or a question mark.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005