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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | 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; } |
