Set C# console application to Unicode output
Asked Answered
E

4

14

I have a C# console application, and I was trying to do some ASCII art within it. However, some of the characters I wanted to use are Unicode. So, I was searching the internet/SO and couldn't find a consolidated answer on how to set the console to be Unicode in a C# console application.

TDLR: How do I set the console in a C# console application to be Unicode?

Edit: I did find this post after searching for something not related to this question.

Eustacia answered 22/7, 2016 at 19:19 Comment(0)
E
27

It turns out that there are multiple things you need to set up in order to make the console display Unicode characters.

  1. Set the console to a Unicode supported font. To do this, run your C# console application once with Console.ReadKey(); so the window stays open. Right click on the title bar of the window and select Properties. These options will persist when debugging through Visual Studio. You might need to use the Default menu instead for persisting the options throughout the system. In the Fonts tab, you need to set the font to Lucida Console. This font supports Unicode characters. The related post can be found here.
  2. Set the console's code page to UTF-8. This one is a bit tricky. Because, you have to execute a command in the console window to change the code page. For whatever reason, this option is not available as a console preference. To do this, you'll need to make a separate cmd.exe process, and use this instead of the normal console provided.

    var cmd = new Process
    {
        StartInfo =
        {
            FileName = "cmd.exe",
            RedirectStandardInput = true,
            RedirectStandardOutput = true,
            CreateNoWindow = true,
            UseShellExecute = false
        }
    };
    cmd.Start();
    
    cmd.StandardInput.WriteLine("chcp 65001");
    cmd.StandardInput.Flush();
    cmd.StandardInput.Close();
    

    The first part of the code above will create a new cmd.exe process. The settings given to StartInfo will make sure that Console is redirected to this new process. The second part of the code sends a command to this console window and runs it. That command, chcp 65001, sets the console's code page to UTF-8. Related posts can be found here and here.

  3. Set the OutputEncoding to UTF-8. This is the only way that Console.WriteLine will actually output Unicode characters. Setting this is very simple.

    Console.OutputEncoding = Encoding.UTF8;
    

    Now, any output from Console will be in Unicode. The related post can be found here.

So, that's it! I hope this information helps someone. :-)

Eustacia answered 22/7, 2016 at 19:19 Comment(2)
If you set both Console.InputEncoding and Console.OutputEncoding to Encoding.UTF8, the code page is changed internally, so no need to start process chcp.Alanis
For specifying content in C# source files, those need to be in UTF-16LE or have BOM . csc.exe compiler reads them as UTF-16LE by default .Natishanative
C
13

Another option is to use P/Invoke to change the code page directly:

class Program
{
    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern bool SetConsoleOutputCP(uint wCodePageID);

    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern bool SetConsoleCP(uint wCodePageID);

    static async Task<int> Main(string[] args)
    {
        SetConsoleOutputCP(65001);
        SetConsoleCP(65001);

        Console.WriteLine("This is how you say hello in Japanese: こんにちは");

        return 0;
    }
}

Output:

Japanese Characters in the console

Colure answered 12/12, 2019 at 15:6 Comment(7)
Does not work on .net framework. Might bis be a thing in the .net core console?Unlock
The code above is .Net Framework. Note that non-ascii characters (like in the screenshot) only show up if the console font supports it. The font set in the console above is NSimSun.Lumbar
This is the solution that worked for me when trying to get Arabic letters in the console output (.NET 5 on windows). Although I changed the value from 65001 to 1256 (the Arabic encoding code page value).Gullet
also want to note that console was at default font. This code caused the console font to change to courier new which supported the Arabic chars.Gullet
I prefer this solution over the accepted answer because it's clean and works out of the box for me (it's essentially the equivalent of running "chcp 65001" in cmd.exe before launching my C# app).Pilgarlic
If you do this, do it before calling any method in the System.Console class. Otherwise it will initialize to other encoding.Alanis
For specifying content in C# source files, those need to be in UTF-16LE or have BOM . csc.exe compiler reads them as UTF-16LE by default .Natishanative
U
0

The solution I prefer is to change the active console code page of my profile to utf-8 using region under control panel. (see picture) Restart and select a font which can display the necessary characters.

Select this tickbox

Urceolate answered 15/3, 2022 at 3:10 Comment(0)
B
0
// VS 2022
// file Program.cs
// Info.CodePage        Info.Name                    Info.DisplayName
//    1200               utf - 16                      Unicode
//    65001              utf-8                        Unicode (UTF-8)

Console.OutputEncoding = System.Text.Encoding.GetEncoding(1200);
Console.InputEncoding  = System.Text.Encoding.GetEncoding(1200);

More about encoding and list of supported codepage

Burne answered 8/2 at 19:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.