gpt4 book ai didi

c# - 为什么我的 C# 代码无法识别版权符号?

转载 作者:行者123 更新时间:2023-11-30 14:11:08 25 4
gpt4 key购买 nike

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

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

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

enter image description here

我哪里出错了?

最佳答案

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 encoding 使用单个字节对版权符号进行编码:

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

关于c# - 为什么我的 C# 代码无法识别版权符号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21140074/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com