Compares the values of two byte arrays, and returns true only if every array member is identical
//-----------------------------------------------------------------------
// <copyright file="Utility.cs" company="ParanoidMike">
// Copyright (c) ParanoidMike. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace ParanoidMike
{
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Microsoft.Win32;
/// <summary>
/// Reusable functions for many uses.
/// </summary>
public static class Utility
{
/// <summary>
/// Compares the values of two byte arrays, and returns true only if every array member is identical
/// </summary>
/// <param name="firstArray">
/// First array to be compared.
/// </param>
/// <param name="secondArray">
/// Second array to be compared.
/// </param>
/// <returns>
/// True if first array is identical to second array.
/// False if the arrays are not identical.
/// </returns>
public static bool DoByteArraysMatch(
byte[] firstArray,
byte[] secondArray)
{
// Check to be sure they two arrays match in length before doing anything else; if not, then they cannot possibly match
int upperBound = firstArray.GetUpperBound(0);
try
{
if (upperBound != secondArray.GetUpperBound(0))
{
// Caller must've screwed something up, as this function is only intended to compare arrays with the same number of elements
throw (new NotSupportedException("EFSConfiguration.EFSConfiguration.MismatchInSize"));
}
}
catch (NullReferenceException)
{
// One of the arrays is null
throw;
}
// Perform comparison of each byte[i] array value; if any comparison fails, the arrays must be unequal
for (int i = 0; i < upperBound; i++)
{
if (firstArray[i] != secondArray[i])
{
////Console.WriteLine("Array contents are dissimilar: element " + i.ToString + " is equal to \"" + firstArray[i].ToString + "\" in ");
////Console.WriteLine("the first array and \"" + secondArray[i].ToString + "\" in the second array.", Environment.NewLine);
return false;
}
}
// If function has made it this far, then the byte arrays are (almost) certainly identical
return true;
}
}
}
Related examples in the same category