static byte[] BitwiseXOR(byte[] result, byte[] matchValue) { if (result.Length == 0) { return matchValue; } byte[] newResult = new byte[matchValue.Length > result.Length ? matchValue.Length : result.Length]; for (int i = 1; i < newResult.Length+1; i++) { //Use XOR on the LSBs until we run out if(i > result.Length) { newResult[newResult.Length - i] = matchValue[matchValue.Length - i]; } else if (i > matchValue.Length) { newResult[newResult.Length - i] = result[result.Length - i]; } else { newResult[newResult.Length -i] = (byte)(matchValue[matchValue.Length - i] ^ result[result.Length - i]); } } return newResult; }
Wednesday, August 31, 2011
Bitwise XOR on Byte Array
Spent the past few hours scrambling for a generic XOR solution on two byte arrays. I needed a solution that would add the match value to a specific result value if it did not already exist (assuming the most significant byte starts at the beginning of the array). You'd think this would be somewhere in the C# libraries by now. After an hour of searching, I decided to write my own. Ended up with the solution below.
Labels:
C#
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment