C#:如何打印unicode字符串到控制台? [英] C#: How to print a unicode string to console?

查看:123
本文介绍了C#:如何打印unicode字符串到控制台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将C#中的unicode String 的值输出到控制台?

  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);

我得到的是?????????



但是,在调试模式下,我在autos和本地窗口中看到下面的值:unicodeString的值,这是我想要显示的值。



慒䑷瑡彡〵〵ㅟㅟ⸷惯



如何将正确的结果打印到控制台上作为自动和本地

我怀疑这些字节真的在 UTF8 ,而不是在你所想的 UTF16



如果您将代码更改为:

  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);

您将获得输出:


application / pcap



How do I print out the value of a unicode String in C# to the 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 autos and local window when in debug mode for the value of unicodeString which is what I wanted to display.

"慒䑷瑡彡〵〶ㅟ⸷慣�"

How do I print out the correct result to the console as what the autos and local window for debugging demonstrated?

解决方案

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

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'll get the output:

application/pcap

这篇关于C#:如何打印unicode字符串到控制台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆