Compares the values of two byte arrays, and returns true only if every array member is identical : Byte Array « File Stream « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# Book
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » File Stream » Byte ArrayScreenshots 
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
1.Convert a byte array to string using default encoding
2.Get the byte array from a string using default encoding
3.Write a byte array to a file
4.Read byte array from file
5.Read Stream to Byte array
6.Save Byte Array To File
7.Reinitializes a byte array to the given value in an optimized way: byteArraySet
8.Bit Array To Byte Array
9.Bit Array To Byte Array (2)
10.Checks if the input byte array contains only safe values, the data does not need to be encoded for use with LDIF
11.Convert byte array into a string
12.Byte Array To String
13.Bytes To Formatted String
14.Get string from byte array
15.Reads a file into a byte array
16.Read entire stream into a byte array
17.Read Stream to fill the byte array
18.Convert to easy-to-read byte value
19.Conversions between complex types and byte arrays.
20.Search a byte array for a sub byte array
21.Append various data types to byte array
22.Convert a byte array to an Object.
23.Convert an object to a byte array.
24.Bytes Equal
25.Compare Bytes
26.Pad bytes.
27.Bytes To Comma Separated List
28.Compare Byte Array Elements
29.Byte Arrays Are Equal
30.Converts a number of bytes into a more easily read form.
31.Int to byte array converter
32.Compares up to n elements of byte array a[] against byte array b[].
33.Utility method to compare two byte arrays for equality
34.Swap Byte Order
35.Get bytes and read bytes
36.Copy as much data as possible for a read operation.
java2s.com  |  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.