c# - 从字节数组中读取行(不将字节数组转换为字符串)

标签 c# .net bytearray networkstream

我有一个从 NetworkStream 读取的字节数组。前两个字节告诉后面的数据包的长度,然后数据包被读入该长度的字节数组。我需要从 NetworkStream/byte 数组中读取的数据有几个字符串,即由换行符终止的可变长度数据,以及一些固定宽度的字段,如字节和长。所以,像这样:

// I would have delimited these for clarity but I didn't want
// to imply that the stream was delimited because it's not.
StringbyteStringStringbytebytebytelonglongbytelonglong

我知道(并且有一些发言权)所遇到的数据包的格式,我需要做的是为每个字符串值读取一个“行”,但是为字节读取固定数量的字节和渴望。到目前为止,我提出的解决方案是使用 while 循环将字节读入临时字节数组,直到出现换行符。然后,将字节转换为字符串。这对我来说似乎很笨拙,但我没有看到另一种明显的方式。我意识到我可以使用 StreamReader.ReadLine(),但这会涉及另一个流,而我已经有一个 NetworkStream。但如果这是更好的解决方案,我会试一试。

我考虑过的另一个选择是让我的后端团队为这些字符串值的长度写入一两个字节,这样我就可以读取长度,然后根据指定的长度读取字符串。

因此,如您所见,我有一些关于如何处理此问题的选项,并且我希望您能就您认为最好的方法发表意见。这是我现在拥有的用于将整个数据包作为字符串读取的代码。接下来就是将数据包的各个字段打散,根据数据包中的数据进行实际需要做的编程工作,创建对象,更新UI等。

string line = null;  
while (stream.DataAvailable)
{  
    //Get the packet length;  
    UInt16 packetLength = 0;  
    header = new byte[2];  
    stream.Read(header, 0, 2);  
    // Need to reverse the header array for BitConverter class if architecture is little endian.  
    if (BitConverter.IsLittleEndian)
        Array.Reverse(header);  
    packetLength = BitConverter.ToUInt16(header,0);

    buffer = new byte[packetLength];
    stream.Read(buffer, 0, BitConverter.ToUInt16(header, 0));
    line = System.Text.ASCIIEncoding.ASCII.GetString(buffer);
    Console.WriteLine(line);
}

最佳答案

就我个人而言

  1. 将 Int16 放在 字符串,所以你知道有多长 他们将会,并且
  2. 使用 IO.BinaryReader 类来 做阅读,它会“阅读”,整数, 字符串、字符等转换为变量,例如BinReader.ReadInt16() 将读取两个字节,返回它们代表的 int16,并在流中移动两个字节

希望这对您有所帮助。

附言使用 ReadString 方法时要小心,它假定字符串前面带有自定义 7 位整数,即它是由 BinaryWriter 类编写的。 以下内容来自本CodeGuru发布

The BinaryWriter class has two methods for writing strings: the overloaded Write() method and the WriteString() method. The former writes the string as a stream of bytes according to the encoding the class is using. The WriteString() method also uses the specified encoding, but it prefixes the string's stream of bytes with the actual length of the string. Such prefixed strings are read back in via BinaryReader.ReadString().

The interesting thing about the length value it that as few bytes as possible are used to hold this size, it is stored as a type called a 7-bit encoded integer. If the length fits in 7 bits a single byte is used, if it is greater than this then the high bit on the first byte is set and a second byte is created by shifting the value by 7 bits. This is repeated with successive bytes until there are enough bytes to hold the value. This mechanism is used to make sure that the length does not become a significant portion of the size taken up by the serialized string. BinaryWriter and BinaryReader have methods to read and write 7-bit encoded integers, but they are protected and so you can use them only if you derive from these classes.

关于c# - 从字节数组中读取行(不将字节数组转换为字符串),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/492454/

相关文章:

C# - 创建未知大小的字节数组?

c# 如何将 csv 转换为 xml

c# - 使用 C# 仅下载网页的第一部分(长度未知)

c# - C# 中的循环按钮背景图像

c# - 使用 64 位项目中的 32 位库 - .NET

actionscript-3 - 声音——播放了多少字节?

c# - 我可以在 ASP 表列中添加按钮并获取在哪一行按钮被单击吗?

c# - Web 服务的 Web 异常

c# - C# (C Sharp) 中的 Apache HTTP 服务器实现

android - 在运行时构建一个 9 patch drawable