How can I use LINQ to to XOR
bytes together in an array? I'm taking the output of an MD5 hash and would like to XOR
every four bytes together so that I can get a 32 bit int
out of it. I could easily do this with a loop, but I thought it was an interesting problem for LINQ.
public static byte[] CompressBytes(byte[] data, int length)
{
byte[] buffer = new byte[length];
for (int i = 0; i < data.Length; i++)
{
for (int j = 0; j < length; j++)
{
if (i * length + j >= data.Length)
break;
buffer[j] ^= data[i * length + j];
}
}
return buffer;
}
Slightly off topic, but is this even a good idea? If I need a int
, would I be better off with a different hash function, or should I just take the first 4 bytes of the MD5 because XOR
ing them all wouldn't help any? Comments are welcome.