This discussion is archived
13 Replies Latest reply: Jun 7, 2008 10:14 AM by 807591 RSS

Compare two byte[]

710081 Newbie
Currently Being Moderated
In someVector I have byte[] elements stored. I retrieve them and I try to compare them with hashByte. But equals method does not work as expected. Is there a
more convenient mode, rather then writing my own iteration and compare byte by byte .. ?
byte [] hashByte = "some fixed length string".getBytes();
for (int k = 0; k < someVector.size(); k++) {
                         Object temp = someVector.get(k);
                         byte[] b = temp.toString().getBytes();
                         if (b.equals(hashByte)) {
                              result = "OK";
                              break;
                         }
                    }
  • 1. Re: Compare two byte[]
    JosAH Newbie
    Currently Being Moderated
    If you just want to compare those arrays for equality have a look at the Arrays utility class; otherwise you're on your own.

    kind regards,

    Jos
  • 2. Re: Compare two byte[]
    710081 Newbie
    Currently Being Moderated
    I am computing hashes and then comparing bytes. Converting to String would be too CPU intensive since I am computing millions of hashes.
    Its pretty obvious that the method I wrote below works but I thought java has some kind of support for comparing simple structures.

              String test = "1921f9e1416473a1d0f15d328e837115";
              byte [] bit = test.getBytes();
              String test2 = "1921f9e1416473a1d0f15d328e837115";
              byte [] bit2 = test2.getBytes();
              
              boolean ok = true;
              
              for (int i = 0; i < bit.length; i++) {
                   if (bit[i] != bit2) {
                        ok = false;
                        break;
                   }
              }
              System.out.println(ok);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
  • 3. Re: Compare two byte[]
    807591 Newbie
    Currently Being Moderated
    Nobody said anything about converting to a String. You already have one. Why don't you just do
    if (test.equals(test2))
    {
    
    }
  • 4. Re: Compare two byte[]
    807591 Newbie
    Currently Being Moderated
    remus.dragos wrote:
    but I thought java has some kind of support for comparing simple structures.
    Let's try an even bigger spoon. Check out the Arrays.equals method!
  • 5. Re: Compare two byte[]
    807591 Newbie
    Currently Being Moderated
    uj_ wrote:
    remus.dragos wrote:
    but I thought java has some kind of support for comparing simple structures.
    Let's try an even bigger spoon. Check out the Arrays.equals method!
    I don't see how the spoon can be any bigger than the one I posted in reply #3 .
  • 6. Re: Compare two byte[]
    807591 Newbie
    Currently Being Moderated
    sabre150 wrote:
    uj_ wrote:
    remus.dragos wrote:
    but I thought java has some kind of support for comparing simple structures.
    Let's try an even bigger spoon. Check out the Arrays.equals method!
    I don't see how the spoon can be any bigger than the one I posted in reply #3 .
    That's a huge spoon but it works only if the data originally is in String format which I have the strong feeling it isn't.
  • 7. Re: Compare two byte[]
    807591 Newbie
    Currently Being Moderated
    uj_ wrote:
    sabre150 wrote:
    uj_ wrote:
    remus.dragos wrote:
    but I thought java has some kind of support for comparing simple structures.
    Let's try an even bigger spoon. Check out the Arrays.equals method!
    I don't see how the spoon can be any bigger than the one I posted in reply #3 .
    That's a huge spoon but it works only if the data originally is in String format which I have the strong feeling it isn't.
    Which reply #2 shows it is! Although the OP dealt with bytes the follow up showed that the bytes came from a hex String.
  • 8. Re: Compare two byte[]
    807591 Newbie
    Currently Being Moderated
    sabre150 wrote:
    Which reply #2 shows it is! Although the OP dealt with bytes the follow up showed that the bytes came from a hex String.
    Unless #2 is some kind of test situation which doesn't correspond to the real setup.

    Anyway, if data originally is in String format then it's best to use equals of String. If it's in byte array format it's better to use Arrays.equals. That's the ultimate spoon. -:)
  • 9. Re: Compare two byte[]
    807591 Newbie
    Currently Being Moderated
    uj_ wrote:
    sabre150 wrote:
    Which reply #2 shows it is! Although the OP dealt with bytes the follow up showed that the bytes came from a hex String.
    Unless #2 is some kind of test situation which doesn't correspond to the real setup.

    Anyway, if data originally is in String format then it's best to use equals of String. If it's in byte array format it's better to use Arrays.equals. That's the ultimate spoon. -:)
    OK. So both Jos and I are right but you are much much much more right!
  • 10. Re: Compare two byte[]
    710081 Newbie
    Currently Being Moderated
    sabre150 wrote:
    Nobody said anything about converting to a String. You already have one. Why don't you just do
    if (test.equals(test2))
    {
    
    }
    Sorry but I dont have the Strings that i exemplified there. I only have byte[] after a MD5 digest for example.
    I did it with Strings to better explain it :)
  • 11. Re: Compare two byte[]
    807591 Newbie
    Currently Being Moderated
    remus.dragos wrote:
    sabre150 wrote:
    Nobody said anything about converting to a String. You already have one. Why don't you just do
    if (test.equals(test2))
    {
    
    }
    Sorry but I dont have the Strings that i exemplified there. I only have byte[] after a MD5 digest for example.
    I did it with Strings to better explain it :)
    So Jos gave you the answer in the first reply and UJ gave you it in spades in reply #4.
  • 12. Re: Compare two byte[]
    710081 Newbie
    Currently Being Moderated
    I found Arrays.equals(byte[], byte[]). Its the same I wrote with a for but I will use it since its safer when considering different charsets.
  • 13. Re: Compare two byte[]
    807591 Newbie
    Currently Being Moderated
    remus.dragos wrote:
    I found Arrays.equals(byte[], byte[]). Its the same I wrote with a for but I will use it since its safer when considering different charsets.
    I don't understand! You say you have byte arrays yet you now talk of 'charsets'. Bytes are not chars.