PDA

View Full Version : Comparing Byte Arrays in Java



akshata
December 6th, 2008, 05:28 PM
Hi,
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

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

http://java.sun.com/j2se/1.5.0/docs/api/java/nio/ByteBuffer.html

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

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