206

I was wondering if it was possible, in a console application, to write characters like using .NET. When I try to write this character, the console outputs a question mark.

0

5 Answers 5

285

It's likely that your output encoding is set to ASCII. Try using this before sending output:

Console.OutputEncoding = System.Text.Encoding.UTF8;

(MSDN link to supporting documentation.)

And here's a little console test app you may find handy:

C#

using System;
using System.Text;

public static class ConsoleOutputTest {
    public static void Main() {
        Console.OutputEncoding = System.Text.Encoding.UTF8;
        for (var i = 0; i <= 1000; i++) {
            Console.Write(Strings.ChrW(i));
            if (i % 50 == 0) { // break every 50 chars
                Console.WriteLine();
            }
        }
        Console.ReadKey();
    }
}

VB.NET

imports Microsoft.VisualBasic
imports System

public module ConsoleOutputTest 
    Sub Main()
        Console.OutputEncoding = System.Text.Encoding.UTF8
        dim i as integer
        for i = 0 to 1000
            Console.Write(ChrW(i))
            if i mod 50 = 0 'break every 50 chars 
                Console.WriteLine()
            end if
        next
    Console.ReadKey()
    End Sub
end module

It's also possible that your choice of Console font does not support that particular character. Click on the Windows Tool-bar Menu (icon like C:.) and select Properties -> Font. Try some other fonts to see if they display your character properly:

picture of console font edit

Sign up to request clarification or add additional context in comments.

13 Comments

Console.OutputEncoding cannot be set to Encoding.Unicode(UTF-16). Encoding.UTF8, however, is possible.
In .NET 4.5 and later also UTF-16 is supported
hm, this doesn't work for me. I'm trying to print out hindi or korean and no luck
You may need for restart the app to see effect after switching between fonts.
@Cel: I've found that NSimSun works for Chinese and Japanese (as well as English).
|
15

I found some elegant solution on MSDN

System.Console.Write('\uXXXX') //XXXX is hex Unicode for character

This simple program writes ℃ right on the screen.

using System;

public class Test
{
    public static void Main()
    {
        Console.Write('\u2103'); //℃ character code
    }
}

3 Comments

That's really neat! However I think the accepted answer still applies - if the font that the console is using does not support unicode characters, I believe this example will not work. I can't check that, however, as I don't have access to a Windows computer at the moment.
Yes, I believe Sam is correct. I for instance was stuck in the fact that the command prompt fonts did not support my character set.
This answer works best for most things I do -- I let the console default to its font, and I use the trick of setting the output type to UTF8 (shown in the accepted answer).
15

Besides Console.OutputEncoding = System.Text.Encoding.UTF8;

for some characters you need to install extra fonts (ie. Chinese).

In Windows 10 first go to Region & language settings and install support for required language: enter image description here

After that you can go to Command Prompt Proporties (or Defaults if you like) and choose some font that supports your language (like KaiTi in Chinese case): enter image description here

Now you are set to go: enter image description here

1 Comment

thanks a lot. Change the Console Font is the fix for me :) (Also set console out put to use UTF-8)
13

Console.OutputEncoding Property

https://learn.microsoft.com/en-us/dotnet/api/system.console.outputencoding

Note that successfully displaying Unicode characters to the console requires the following:

  • The console must use a TrueType font, such as Lucida Console or Consolas, to display characters.

Comments

5

This works for me:

Console.OutputEncoding = System.Text.Encoding.Default;

To display some of the symbols, it's required to set Command Prompt's font to Lucida Console:

  1. Open Command Prompt;

  2. Right click on the top bar of the Command Prompt;

  3. Click Properties;

  4. If the font is set to Raster Fonts, change it to Lucida Console.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.