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 ...

Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.