103

I have a byte array with a ~known binary sequence in it. I need to confirm that the binary sequence is what it's supposed to be. I have tried .equals in addition to ==, but neither worked.

byte[] array = new BigInteger("1111000011110001", 2).toByteArray();
if (new BigInteger("1111000011110001", 2).toByteArray() == array){
    System.out.println("the same");
} else {
    System.out.println("different'");
}
Vinay Veluri
  • 6,671
  • 5
  • 32
  • 56
Roger
  • 1,461
  • 5
  • 14
  • 15
  • can you just compare the strings directly? – objects Mar 26 '11 at 02:51
  • 1
    @objects - leading zeros. Besides, the String / BigInteger stuff could just be a way of illustrating the byte-array comparison question. – Stephen C Mar 26 '11 at 03:13
  • Have you tried using the compareTo method? BTW `==` compares primitive values just fyi – ChriskOlson Feb 13 '14 at 00:48
  • Here is related question about partial array compare: http://stackoverflow.com/questions/16646967/java-util-arrays-equals-with-limited-length – Vadzim Sep 16 '15 at 08:09

6 Answers6

175

In your example, you have:

if (new BigInteger("1111000011110001", 2).toByteArray() == array)

When dealing with objects, == in java compares reference values. You're checking to see if the reference to the array returned by toByteArray() is the same as the reference held in array, which of course can never be true. In addition, array classes don't override .equals() so the behavior is that of Object.equals() which also only compares the reference values.

To compare the contents of two arrays, static array comparison methods are provided by the Arrays class

byte[] array = new BigInteger("1111000011110001", 2).toByteArray();
byte[] secondArray = new BigInteger("1111000011110001", 2).toByteArray();
if (Arrays.equals(array, secondArray))
{
    System.out.println("Yup, they're the same!");
}
Brian Roach
  • 76,169
  • 12
  • 136
  • 161
64

Check out the static java.util.Arrays.equals() family of methods. There's one that does exactly what you want.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
11

You can use both Arrays.equals() and MessageDigest.isEqual(). These two methods have some differences though.

MessageDigest.isEqual() is a time-constant comparison method and Arrays.equals() is non time-constant and it may bring some security issues if you use it in a security application.

The details for the difference can be read at Arrays.equals() vs MessageDigest.isEqual()

PixelsTech
  • 3,229
  • 1
  • 33
  • 33
11

Java doesn't overload operators, so you'll usually need a method for non-basic types. Try the Arrays.equals() method.

Sumit Singh
  • 15,743
  • 6
  • 59
  • 89
jswolf19
  • 2,303
  • 15
  • 16
4

Of course, the accepted answer of Arrays.equal( byte[] first, byte[] second ) is correct. I like to work at a lower level, but I was unable to find a low level efficient function to perform equality test ranges. I had to whip up my own, if anyone needs it:

public static boolean ArraysAreEquals(
 byte[] first,
 int firstOffset,
 int firstLength,
 byte[] second,
 int secondOffset,
 int secondLength
) {
    if( firstLength != secondLength ) {
        return false;
    }

    for( int index = 0; index < firstLength; ++index ) {
        if( first[firstOffset+index] != second[secondOffset+index]) {
            return false;
        }
    }

    return true;
}
Bamaco
  • 592
  • 9
  • 25
  • This is a good solution for testing a subset of the arrays rather than the whole thing. However, note that Arrays.equals(byte[], byte[]) does pretty much exactly everything you did here (except handles the two values being the same object more efficiently, and handles null arrays being passed in gracefully). When given the choice to use a standard library implementation which will be supported by the community or writing a custom implementation of it which I'll need to support forever, I'll chose the former every time. – Tom Dibble Aug 27 '15 at 21:03
  • 4
    The answer is useful for the use case where one has two arrays and want to compare range of bytes from them, without first making copies of the arrays. Array copies add overhead, add garbage, and are not needed. What I needed was a low-level c style memcmp() and this fits the need. Of course, memcmp() takes only 1 length argument. This function is close enough. – Bamaco Oct 29 '15 at 14:30
2

Since I wanted to compare two arrays for a unit Test and I arrived on this answer I thought I could share.

You can also do it with:

@Test
public void testTwoArrays() {
  byte[] array = new BigInteger("1111000011110001", 2).toByteArray();
  byte[] secondArray = new BigInteger("1111000011110001", 2).toByteArray();

  Assert.assertArrayEquals(array, secondArray);
}

And you could check on Comparing arrays in JUnit assertions for more infos.

Sylhare
  • 5,907
  • 8
  • 64
  • 80