32

I have a function to convert a string to a Unicode string:

private string UnicodeString(string text)
{
    return Encoding.UTF8.GetString(Encoding.ASCII.GetBytes(text));
}

But when I am calling this function the output result is wrong. It looks like my function is not working.

Console.WriteLine(UnicodeString("добры дзень")) printing on console just questions like that: ????? ????

Is there a way to say to console to display it correct?

UPDATE

It looks like the problem not in Unicode. I think maybe it is displaying question marks because I am not having the correct locale in the system (Windows 7)?

Is there a way to make it work without changing locale?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Reg
  • 517
  • 3
  • 7
  • 16

3 Answers3

54

First, change the output encoding to UTF8:

Console.OutputEncoding = Encoding.UTF8;
Console.WriteLine("добры дзень");

Now you'll still see question marks. The reason is that the default console's font doesn't support Cyrillic letters. Change the font of the console:

enter image description here

If you're lucky, you should find a different font with Unicode support:

enter image description here

Change the font, and you should be able to see your text:

enter image description here

In the general case, if you want to display all Unicode characters reliably, the Console is probably not right for you. See also: C# console font (the comments are interesting too)

Community
  • 1
  • 1
Kobi
  • 135,331
  • 41
  • 252
  • 292
1
private string UnicodeString(string text)
{
    return text;
}

The string text is already in Unicode. All internal C# strings are Unicode. When you convert it to ASCII you lose characters. That is why you get ????? ????.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Albin Sunnanbo
  • 46,430
  • 8
  • 69
  • 108
0

Just do plain simple Console.WriteLine("добры дзень"); no need for any conversion.

EliotVU
  • 61
  • 7
  • 3
    It was like that initially, it won't work, the output just question marks. – Reg Feb 20 '11 at 06:56
  • 3
    It's probably the Console object printing in ASCII then. Try changing the OutputEncoding property e.g. Console.OutputEncoding = Encoding.UTF8; – EliotVU Feb 20 '11 at 07:09