在流上生成异或校验和的简单方法
本文关键字:校验和 校验 和的 简单 方法 | 更新日期: 2023-09-27 17:56:31
在C#中,有没有一种简单的方法可以在MemoryStream(二进制)上生成XOR校验和,不包括第一个和最后两个字节?
另外,扩展 BinaryWriter 并在编写流时执行此操作是否更容易?
您可以使用 LINQ 获取答案:
var checksum = memStream
.GetBuffer() // Get the underlying byte array
.Skip(1) // Skip the first byte
.Take(memStream.Length-3) // One for the beginning, two more for the end
.Aggregate(0, (p,v) => p ^ v); // XOR the accumulated value and the next byte