简体   繁体   中英

Checking equality for two byte arrays

I am checking the equality of two byte arrays, and I wanted some help because what I have returns false even though the arrays should be equal.

Within my debug I could see both of a1 and b1 are equal, but it is not going inside the while loop to increment i.

public bool Equality(byte[] a1, byte[] b1)
{
    int i;
    bool bEqual;
    if (a1.Length == b1.Length)
    {
        i = 0;
        while ((i < a1.Length) && (a1[i]==b1[i]))
        {
            i++;
        }

        if (i == a1.Length)
        {
            bEqual = true;
        }
    }
    return bEqual;
}

This always returns false: (a1[i]==b1[i]) .

You need to add a return value somewhere. This should work:

public bool Equality(byte[] a1, byte[] b1)
{
   int i;
   if (a1.Length == b1.Length)
   {
      i = 0;
      while (i < a1.Length && (a1[i]==b1[i])) //Earlier it was a1[i]!=b1[i]
      {
          i++;
      }
      if (i == a1.Length)
      {
          return true;
      }
   }

   return false;
}

But this is much simpler:

return a1.SequenceEqual(b1);

Alternatively, you could use IStructuralEquatable from .NET 4:

return ((IStructuralEquatable)a1).Equals(b1, StructuralComparisons.StructuralEqualityComparer)

If performance is a concern, I'd recommend rewriting your code to use the Binary class, which is specifically optimized for this kind of use case:

public bool Equality(Binary a1, Binary b1)
{
    return a1.Equals(b1);
}

A quick benchmark on my machine gives the following stats:

Method                   Min         Max         Avg
binary equal:          0.868       3.076       0.933    (best)
for loop:              2.636      10.004       3.065
sequence equal:        8.940      30.124      10.258
structure equal:     155.644     381.052     170.693

Download this LINQPad file to run the benchmark yourself.

要检查相等性,您可以写:

var areEqual =  a1.SequenceEqual(b1);

I'd recommend some short-circuiting to make things a bit simpler, and use of object.ReferenceEquals to short-circuit for cases when the arrays are the same reference ( a1 = b1 ):

public bool Equality(byte[] a1, byte[] b1)
{
    // If not same length, done
    if (a1.Length != b1.Length)
    {
        return false;
    }

    // If they are the same object, done
    if (object.ReferenceEquals(a1,b1))
    {
        return true;
    }

    // Loop all values and compare
    for (int i = 0; i < a1.Length; i++)
    {
        if (a1[i] != b1[i])
        {
            return false;
        }
    }

    // If we got here, equal
    return true;
}

This should work:

public bool Equality(byte[] a1, byte[] b1)
{
   if(a1 == null || b1 == null)
       return false;
   int length = a1.Length;
   if(b1.Length != length)
      return false;
   while(length >0) {
       length--;
       if(a1[length] != b1[length])
          return false;           
   }
   return true;        
}

You should add some return statements:

public bool Equality(byte[] a1, byte[] b1)
{
    int i = 0;
    if (a1.Length == b1.Length)
    {
        while ((i < a1.Length) && (a1[i]==b1[i]))
        {
            i++;
        }
    }
    return i == a1.Length;
}

Or, better yet

public bool Equality(byte[] a1, byte[] b1)
{
    if(a1.Length != b1.Length)
    {
        return false;
    }

    for (int i = 0; i < a1.Length; i++)
    {
        if (a1[i] != b1[i])
        {
            return false;
        }
    }
    return true;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM