Comparing two bytes [] for equality in Scala (checking binary image data)

I want to compare two (small) Byte[]

that contain a binary image representation. I don't want to use MD5 or SHA or whatever, because it doesn't make sense ... they just iterate over the array, calculate the checksum, etc. and there is no need.

There seems to be a super-easy way to iterate through two arrays a1

and a2

and compare them for equality, e.g .:

(a1, a2).forall(a, b => a == b)

      

But that doesn't work of course ...

+3


source to share


3 answers


The 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 

      



Another way would be

a.sameElements(b)

      

+7


source


 val arrayOne = Array(1,2,3)
 val arrayTwo = Array(1,2,3)

 arrayOne zip arrayTwo forall {case (a,b) => a == b}

      



+2


source


Consider also the difference between a1

and a2

,

(a1 diff a2).isEmpty

      

which stops the comparison at the first mismatch.

+2


source







All Articles