195

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.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Sam
  • 3,070
  • 3
  • 20
  • 26

5 Answers5

263

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

Paul Sasik
  • 79,492
  • 20
  • 149
  • 189
  • thanks, the "console.outputEncoding" was what i was looking for, but unfortunately i dont think the character was supported by my font. can you expand on the windows toolbar menu thing? thanks for the quick reply – Sam Apr 21 '11 at 22:07
  • Yes, it's the little icon in the upper, left-hand corner of your console window that looks like a tiny version of a console. Every Windows window (that has a title and border) has this. Just click on it to see a menu, one of the options will be Properties. Click on that and find the Font tab to tweak your fonts. – Paul Sasik Apr 21 '11 at 22:10
  • 3
    `Console.OutputEncoding` cannot be set to `Encoding.Unicode`(UTF-16). `Encoding.UTF8`, however, is possible. – Saeb Amini Jan 11 '12 at 07:17
  • 3
    In .NET 4.5 and later also UTF-16 is supported – Sami Kuhmonen Dec 12 '13 at 07:31
  • 6
    hm, this doesn't work for me. I'm trying to print out hindi or korean and no luck – Quincy Jan 28 '15 at 20:51
  • 1
    You may need for restart the app to see effect after switching between fonts. – Mike Keskinov Feb 17 '15 at 19:49
  • 1
    Looks like setting the encoding and using Consolas works for Russian letters (after restart of the console app; with .NET 4.5.1), but not for Korean nor for Chinese.. And cannot seem to choose any fonts that work for these.. – Cel Apr 17 '15 at 08:56
  • Be aware even after you set Encoding and choose fonts (not many to choose from), there are still characters that cannot be displayed. I actually had to change to GUI to finally solve the issue. – Gildor Sep 16 '15 at 22:34
  • 1
    @Gildor: you are right, but you might be able to find a monospaced font that does support your character set. See [this](http://www.codeproject.com/Articles/30040/Font-Survey-42-of-the-Best-Monospaced-Programming) – Veverke Sep 21 '15 at 12:07
  • The C# example will not work unless you add a reference to `Microsoft.VisualBasic.dll` and add a using for `Microsoft.VisualBasic`. – Ian Kemp Jun 25 '16 at 12:36
  • 2
    @Cel: I've found that NSimSun works for Chinese and Japanese (as well as English). – kjhughes May 04 '18 at 13:23
  • `Console.OutputEncoding = Encoding.UTF8` and the `SimHei` font made it possible to display chinese characters for me. 谢谢! – Maxim Zhukov Jul 26 '19 at 13:55
  • 1
    char.ConvertFromUtf32() instead of Strings.ChrW() for C#.. Strings.ChrW() is a VB Call. – Chizl Aug 25 '20 at 12:33
14

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
    }
}
Destiner
  • 540
  • 4
  • 16
  • 2
    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. – Sam Mar 09 '15 at 21:44
  • 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. – Veverke Sep 21 '15 at 10:40
  • 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). – Su Llewellyn Jun 01 '23 at 18:30
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.
Pang
  • 9,564
  • 146
  • 81
  • 122
Pažout
  • 2,061
  • 20
  • 10
13

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

watbywbarif
  • 6,487
  • 8
  • 50
  • 64
  • thanks a lot. Change the Console Font is the fix for me :) (Also set console out put to use UTF-8) – Franva Mar 10 '20 at 13:14
1

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.