3

C# では、最初と最後の 2 バイトを除いて、MemoryStream (バイナリ) で XOR チェックサムを生成する簡単な方法はありますか?

また、BinaryWriter を拡張し、Stream が書き込まれるときにそれを行う方が簡単ですか?

4

1 に答える 1

4

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
于 2013-01-30T01:52:40.990 に答える