I want to compare two (small) Byte[]
's that contain a representation of a binary image. I don't want to use MD5 or SHA or whatnot because there is no point... these just iterate the array, calculate a checksum, etc., and there is no need.
It seems there should be a super-easy way to iterate of two arrays, a1
and a2
, and compare them for equality, such as:
(a1, a2).forall(a, b => a == b)
But this does not work of course...
equals(byte[] a, byte[] a2) method returns true if the two specified arrays of bytes are equal to one another. Two arrays are equal if they contain the same elements in the same order. Two array references are considered equal if both are null.
The compare() method of Byte class is a built in method in Java which is used to compare two byte values. Parameters: It takes two byte value 'a' and 'b' as input in the parameters which are to be compared. Return Value: It returns an int value.
You can simply iterate the byte array and print the byte using System. out. println() method.
Following should do it
val a: Array[Byte] = Array(1,2,4,5)
val b: Array[Byte] = Array(1,2,4,5)
a.deep==b.deep
The other way would be
a.sameElements(b)
Consider also the difference between a1
and a2
,
(a1 diff a2).isEmpty
which halts the comparing at the first mismatch.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With