简体   繁体   English

为什么我的c#代码不识别版权符号?

[英]Why doesn't my c# code recognize copyright symbol?

byte[] newBytes = new Byte[] { 169 };
string string1 = System.Text.Encoding.UTF8.GetString(newBytes, 0, newBytes.Length);

In the above program, I expected string1 to have the value of copyright symbol ©. 在上面的程序中,我希望string1具有版权符号©的值。

But I get some other value (possibly some junk) as shown below 但我得到一些其他价值(可能是一些垃圾),如下所示

在此输入图像描述

Where did I go wrong? 我哪里做错了?

UTF8 requires multiple bytes to encode character points greater than 127. If you run the reverse, you'll see what it expects: UTF8需要多个字节来编码大于127的字符点。如果你反向运行,你会看到它所期望的:

System.Text.Encoding.UTF8.GetBytes("©"); // { 194, 169 }

Try this: 试试这个:

byte[] newBytes = new Byte[] { 194, 169 };
string string1 = System.Text.Encoding.UTF8.GetString(newBytes, 0, newBytes.Length);

If you absolutely have to use that original byte array, you'll need to pick a different encoding. 如果您必须使用该原始字节数组,则需要选择不同的编码。 For example, the Windows-1252 encoding uses a single byte to encode the copyright symbol: 例如, Windows-1252编码使用单个字节对版权符号进行编码:

byte[] newBytes = new Byte[] { 169 };
var encoding = Encoding.GetEncoding(1252);
string string1 = encoding.GetString(newBytes, 0, newBytes.Length); // "©"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM