Compares the elements in this instance with the specified byte array and indicates whether they are equal. - CSharp System

CSharp examples for System:Byte Array

Description

Compares the elements in this instance with the specified byte array and indicates whether they are equal.

Demo Code

// Distributed under the Boost Software License, Version 1.0.
using System.Text;
using System.Collections.Generic;
using System;//w ww.j a va 2  s  .c  om

public class Main{
        /// <summary>
        ///     Compares the elements in this instance with the specified byte
        ///     array and indicates whether they are equal.
        /// </summary>
        /// <param name="source">The source array.</param>
        /// <param name="array">The array to compare to.</param>
        /// <returns>True if the arrays are equal; false otherwise.</returns>
        public static unsafe bool CompareTo(this byte[] source, byte[] array)
        {
            if (source == null && array == null)
                return true;

            int arrayALength = source.Length;

            if (source == null || array == null || arrayALength != array.Length)
                return false;

            fixed (byte* pArrayA = source)
            fixed (byte* pArrayB = array)
            {
                long* pPosA = (long*)pArrayA;
                long* pPosB = (long*)pArrayB;
                int end = arrayALength >> 3;

                for (int i = 0; i < end; i++, pPosA += 8, pPosA += 8)
                {
                    if (*pPosA != *pPosB)
                        return false;
                }

                if ((end & 4) != 0)
                {
                    if (*(int*)pPosA != *(int*)pPosB)
                        return false;
                    pPosA += 4;
                    pPosB += 4;
                }

                if ((end & 2) != 0)
                {
                    if (*(short*)pPosA != *(short*)pPosB)
                        return false;
                    pPosA += 2;
                    pPosB += 2;
                }

                if ((end & 1) != 0)
                    if (*(byte*)pPosA != *(byte*)pPosB)
                        return false;

                return true;
            }
        }
}

Related Tutorials