Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show UTF-8 characters in console

Tags:

c#

How can I print UTF8 characters in the console?

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

like image 691
Adrya Avatar asked Jan 14 '10 08:01

Adrya


People also ask

How do I know if a character is UTF-8?

You do that by calling str. valid_encoding? on a String str that is in UTF-8 -encoding. Does that not get clear from my answer? Programmatically, you can not (or at least not easily and of course not reliably) check the invalidity of a string in a one-byte-encoding such as CP1252 .

Can UTF-8 represent all characters?

Each UTF can represent any Unicode character that you need to represent. UTF-8 is based on 8-bit code units. Each character is encoded as 1 to 4 bytes. The first 128 Unicode code points are encoded as 1 byte in UTF-8.


2 Answers

Console.OutputEncoding = Encoding.UTF8; 
like image 98
Ozgur Avatar answered Sep 19 '22 17:09

Ozgur


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.

like image 23
Reed Copsey Avatar answered Sep 17 '22 17:09

Reed Copsey