【发布时间】:2016-01-05 21:02:40
【问题描述】:
我用 C# 编写了应用程序,该应用程序使用套接字(udp libenet)通过网络从服务器接收数据。
在我的应用程序中,我具有处理数据包中发送的原始字节的功能。 功能之一是读取字符串,由 \0 分隔。
我的问题是我通过服务器将 UTF-8 编码的字符串发送到 C# 应用程序,但是当我使用这些字符串在控件中显示它们时,我得到的是乱码而不是波兰字母。
从缓冲区读取字符串的函数:
public override string ReadString()
{
StringBuilder sb = new StringBuilder();
while (true)
{
byte b;
if (Remaining > 0)
b = ReadByte();
else
b = 0;
if (b == 0) break;
// Probably here is the problem. Checked other encodings etc., but still same
sb.Append(Encoding.UTF8.GetString(new byte[] { b }, 0, 1));
}
return sb.ToString();
}
函数覆盖,来自:
public class BitReader : BinaryReader
在我的应用程序中,我得到:
【问题讨论】:
-
stackoverflow.com/questions/30523775/… 中提供了合理的可复制粘贴代码。