Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 172
  • Last Modified:

WHy the byte comparison doesn't work?

HI expert,

I have to byte array and they have the same content.  WHy the below codes

first16Byte[] = ...
second16Byte[] = ...
 boolean byteCompare = first16Byte.equals(second16Byte);
Why byteCompare always return false even thought the byte array content and size are identicial?
0
4eyesgirl
Asked:
4eyesgirl
  • 2
1 Solution
 
UrosVidojevicCommented:
That is not correct way of comparing two arrays.

Try:

Arrays.equals(first16Byte, second16Byte);

=====================================

public static boolean equals(byte[] a,
                             byte[] a2)

    Returns true if the two specified arrays of bytes are equal to one another. Two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. In other words, two arrays are equal if they contain the same elements in the same order. Also, two array references are considered equal if both are null.

    Parameters:
        a - one array to be tested for equality.
        a2 - the other array to be tested for equality.
    Returns:
        true if the two arrays are equal.

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now