The Artima Developer Community
Sponsored Link

Java Answers Forum
compare byte arrays?

1 reply on 1 page. Most recent reply: Feb 10, 2003 4:12 AM by RANGAMANA Hermann

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Frank

Posts: 1
Nickname: horse
Registered: Feb, 2003

compare byte arrays? Posted: Feb 9, 2003 9:21 PM
Reply to this message Reply
Advertisement
Hi All,
This is probably a very basic question, but I'm looking for an efficent way to compare two byte arrays. Lets say you have some code like the following:

boolean
compare_byte_array(byte[] b1, byte[] b2)
{
if (b1.length != b2.length)
{
return false;
}
for (int i=0; i < b1.length; i++)
{
if (b1 != b2)
{
return false;
}
}
return true;
}

Surley there must be a more efficent way of implementing this? Is there a way to do a memory comparison - if I were implementing this in c++ I'd use memcmp() in place of that for loop.

BTW would the following do the trick -
boolean
compare_byte_array(byte[] b1, byte[] b2)
{
return (b1 == b2);
}

what exactly gets compared here - does this do a deep comparison of each element? or does this simply check that b1 and b2 are indeed the same obects.


RANGAMANA Hermann

Posts: 5
Nickname: hrangamana
Registered: Feb, 2003

Re: compare byte arrays? Posted: Feb 10, 2003 4:12 AM
Reply to this message Reply
Hi,

take a look at
java.util.Arrays.equals (byte[] b, byte[] b2)

... it compares two arrays of byte. I don't know how efficient is this method, wether it loops over the two arrays and compares each correspondig elements (wich is indeed a not so efficient implementation) or does some internal memory comparison ...

/hermann

Flat View: This topic has 1 reply on 1 page
Topic: JDBC-ODBC Bridge Previous Topic   Next Topic Topic: Relationship fields in EJB

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use