【问题标题】:Why doesn't my c# code recognize copyright symbol?为什么我的 c# 代码不能识别版权符号?
【发布时间】:2014-02-04 01:53:05
【问题描述】:
byte[] newBytes = new Byte[] { 169 };
string string1 = System.Text.Encoding.UTF8.GetString(newBytes, 0, newBytes.Length);

在上面的程序中,我希望 string1 具有版权符号 © 的值。

但我得到了一些其他值(可能是一些垃圾),如下所示

我哪里做错了?

【问题讨论】:

标签: c# unicode utf-8


【解决方案1】:

UTF8 需要多个字节来编码大于 127 的字符点。如果你反过来运行,你会看到它所期望的:

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

试试这个:

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

如果您绝对必须使用该原始字节数组,则需要选择不同的编码。例如,Windows-1252 编码使用单个字节来编码版权符号:

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

【讨论】:

    猜你喜欢
    • 2015-01-21
    • 2015-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-30
    • 1970-01-01
    相关资源
    最近更新 更多