public static class ByteArrayHelper { // Output the byte array as a C# style byte array variable declaration // where the variable name of the byte array is passed as a parameter public static string ToCSharpByteArrayDeclrn( this byte[] bytes, string name) { string res = "byte[] " + name + " = new byte[" + bytes.Length.ToString() + "] { "; int ix = 0; for (; ix < bytes.Length - 1; ix++) { res += "0x" + bytes[ix].ToString("X2") + ", "; } for (; ix < bytes.Length; ix++) { res += "0x" + bytes[ix].ToString("X2"); } res += " };"; return res; } // Checks whether 2 byte arrays are exactly the same, ie. they have // the same length and the same values at each array entry public static bool AreTheSame(this byte[] bytes1, byte[] bytes2) { bool areTheSame = (bytes1.Length == bytes2.Length); for (int ix = 0; (ix < bytes1.Length) && areTheSame; ix++) { areTheSame = bytes1[ix] == bytes2[ix]; } return areTheSame; } // XOR together 2 byte arrays public static byte[] XOr(this byte[] bytes, byte[] xorBytes) { Debug.Assert(bytes.Length == xorBytes.Length, $"Byte arrays are different sizes: first is {bytes.Length} " + $"and the second is {xorBytes.Length}"); byte[] res = new byte[bytes.Length]; int len = xorBytes.Length; for (int ix = 0; ix < bytes.Length; ix++) { res[ix] = (byte)(bytes[ix] ^ xorBytes[ix % len]); } return res; } }
January 18, 2009
Useful Byte Array Debugging Classes
Some useful debugging classes for debugging byte arrays. Useful for unit tests.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment