View Full Version : Comparing Byte Arrays in Java

December 6th, 2008, 05:28 PM
I need to compare two Byte Arrays in Java and check if one is greater than the other or lesser. Can this be done? I am a novice in Java and need help.

Thannk You

December 6th, 2008, 06:46 PM
Either loop through the arrays and compare them byte by byte, or wrap them in ByteBuffers and use ByteBuffer's compareTo() method.


Look at the wrap() and compareTo() methods.

December 7th, 2008, 07:16 AM
The BitArray is the one API class I have noticed that does change on 64-bit or 32-bit computers doing java SDK. On my machine I get 64 placeholders for the bits init to FALSE using the 32-bit JRE. You MUST load a BitArray with values. You can get the Cardinality of the BitArray to speed things up with transversing the Array (iterator).

Having said that statement above, you probably want ByteBuffer so you get Comparator. I have had ByteBuffer compare correctly on 32-bit and 64-bit platforms.