C #: How to print Unicode string for console?

How to print unicode value Stringin c # on console?

byte[] unicodeBytes = new byte[] 
    {0x61, 0x70, 0x70, 0x6C, 0x69, 0x63, 0x61, 
     0x74, 0x69, 0x6F, 0x6E, 0x2F, 0x70, 0x63, 
     0x61, 0x70};

string unicodeString = Encoding.Unicode.GetString(unicodeBytes);

Console.WriteLine(unicodeString);

What I get for the above is "?????????"

However, I see the following in the auto and local window in debug mode for the unicodeString value that I wanted to display.

"慒 䑷 瑡 彡 〵 〶 ㅟ ⸷ 慣"

How to print the correct result on the console, as demonstrated by the auto and local debugging window?

+3
source share
2 answers

I suspect that these bytes are really in UTF8, not in UTF16, as you expected.

See if you change your code to:

byte[] unicodeBytes = new byte[] 
    {0x61, 0x70, 0x70, 0x6C, 0x69, 0x63, 0x61, 
     0x74, 0x69, 0x6F, 0x6E, 0x2F, 0x70, 0x63, 
     0x61, 0x70};

string unicodeString = Encoding.UTF8.GetString(unicodeBytes);

Console.WriteLine(unicodeString);

You will get the result:

Application / PCAP

+3

. : unicode #

:

Console.OutputEncoding = Encoding.UTF8;

Lucida Console, Unicode.

+1

Source: https://habr.com/ru/post/1741115/


All Articles