C# Unicode string output

前端 未结 3 586
误落风尘
误落风尘 2020-11-27 07:34

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

private string UnicodeString(string text)
{
    return Encoding.UTF8.GetString(Encoding.ASCII.GetB         


        
相关标签:
3条回答
  • 2020-11-27 07:41
    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 ????? ????.

    0 讨论(0)
  • 2020-11-27 07:52

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

    0 讨论(0)
  • 2020-11-27 07:58

    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)

    0 讨论(0)
提交回复
热议问题