O'Reilly logo

Microsoft® Visual Basic® .NET Programmer's Cookbook by Matthew MacDonald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Test Byte Arrays for Equality

Problem

You want to compare the bytes in two byte arrays to see if they have the same information.

Solution

Iterate over the array and compare each byte, or use the BitConverter.ToString method to create a string representation of the entire array.

Discussion

There is no way to directly test to arrays for equal content. You can use the Is operator, but this will only return True if both variables point to the same array. It will fail if the arrays are duplicate copies of identical data.

If Array1 Is Array2 Then
    ' This is a reference comparison, which only tests whether
    ' the variables reference the same array object.
End If

A shortcut is to use the BitConverter.ToString method to put both byte arrays into a standard string ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required