By Sam


2011-04-21 21:55:37 8 Comments

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.

3 comments

@Paul Sasik 2011-04-21 21:58:39

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

@Sam 2011-04-21 22:07:48

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

@Paul Sasik 2011-04-21 22:10:58

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.

@Sam 2011-04-21 22:14:49

I didn't know about that, thanks again for the help.

@Saeb Amini 2012-01-11 07:17:46

Console.OutputEncoding cannot be set to Encoding.Unicode(UTF-16). Encoding.UTF8, however, is possible.

@Sami Kuhmonen 2013-12-12 07:31:09

In .NET 4.5 and later also UTF-16 is supported

@Quincy 2015-01-28 20:51:30

hm, this doesn't work for me. I'm trying to print out hindi or korean and no luck

@Mike Keskinov 2015-02-17 19:49:44

You may need for restart the app to see effect after switching between fonts.

@Cel 2015-04-17 08:56:42

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

@Gildor 2015-09-16 22:34:25

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.

@Veverke 2015-09-21 12:07:52

@Gildor: you are right, but you might be able to find a monospaced font that does support your character set. See this

@Ian Kemp 2016-06-25 12:36:41

The C# example will not work unless you add a reference to Microsoft.VisualBasic.dll and add a using for Microsoft.VisualBasic.

@WebComer 2017-05-27 12:00:21

It would be good to show us the way to set console properties by program, not by hand each and every time.

@kjhughes 2018-05-04 13:23:26

@Cel: I've found that NSimSun works for Chinese and Japanese (as well as English).

@Destiner 2014-09-14 12:37:01

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
    }
}

@Sam 2015-03-09 21:44:05

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.

@Veverke 2015-09-21 10:40:45

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.

@Pažout 2014-04-09 09:33:19

Console.OutputEncoding Property

http://msdn.microsoft.com/library/system.console.outputencoding(v=vs.110).aspx

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

Related Questions

Sponsored Content

10 Answered Questions

[SOLVED] What's the @ in front of a string in C#?

  • 2009-02-17 09:54:17
  • Klaw
  • 186398 View
  • 481 Score
  • 10 Answer
  • Tags:   c# .net string

38 Answered Questions

26 Answered Questions

[SOLVED] How do I enumerate an enum in C#?

11 Answered Questions

[SOLVED] How do I specify the exit code of a console application in .NET?

  • 2008-09-30 23:45:10
  • MrDatabase
  • 260612 View
  • 414 Score
  • 11 Answer
  • Tags:   c# .net exit-code

33 Answered Questions

[SOLVED] What is a NullReferenceException, and how do I fix it?

15 Answered Questions

25 Answered Questions

[SOLVED] How can I get the application's path in a .NET console application?

5 Answered Questions

[SOLVED] Try-catch speeding up my code?

1 Answered Questions

[SOLVED] Escape curly brace '{' in String.Format

  • 2010-09-22 21:44:19
  • PhilB
  • 169559 View
  • 807 Score
  • 1 Answer
  • Tags:   c# .net string

Sponsored Content