I am trying to convert byte[] to Hex string and same Hex string to byte[] in android , data got mismatched.
Ex :
Received byte[] data : [B@b39c86a
converted Hex string : 8be897cc3c4d9e5dd6a6bbd106d8e8d487691b56
When I decode the hex string, I am getting [B@ea6d15b, but its should be [B@b39c86a
I am using below code to convert.
public String byte2hex(byte[] a) {
/*StringBuilder sb = new StringBuilder(a.length * 2);
for (byte b : a)
sb.append(String.format("%02x", b & 0xff));
return sb.toString();*/
String hexString = "";
for(int i = 0; i < a.length; i++){
String thisByte = "".format("%x", a[i]);
hexString += thisByte;
}
return hexString;
}
public static byte[] hexStringToByteArray(String s) {
/* int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i + 1), 16));
}
return data;*/
byte[] bytes = new byte[s.length() / 2];
for(int i = 0; i < s.length(); i += 2){
String sub = s.substring(i, i + 2);
Integer intVal = Integer.parseInt(sub, 16);
bytes[i / 2] = intVal.byteValue();
String hex = "".format("0x%x", bytes[i / 2]);
}
return bytes;
}
I assume you are getting the [B@b39c86a by printing the object. Those numbers are generated by the toString() method and do not tell you anything about the contents of the array.
For example if you run this code:
byte[] arr = new byte[]{1,2,3,4};
byte[] arr2 = new byte[4];
for(int i=0; i < 4; i++)
arr2[i] = arr[i];
System.out.println("Array 1: " + arr);
System.out.println("Array 2: " + arr2);
System.out.println("arr.equals: " + arr.equals(arr2));
System.out.println("Arrays.equals: " + Arrays.equals(arr,arr2));
System.out.printf("Contents of array 1: %s\n", Arrays.toString(arr));
System.out.printf("Contents of array 2: %s\n", Arrays.toString(arr2));
The output would for example be:
Array 1: [B@74a14482
Array 2: [B@1540e19d
arr.equals: false
Arrays.equals: true
Contents of array 1: [1, 2, 3, 4]
Contents of array 2: [1, 2, 3, 4]
the toString method is in the format ClassName @ hashCode. The hashCode when not implemented for a specific class (As is the case for a byte-array).
If you look at the Javadoc it states:
/**
....
* As much as is reasonably practical, the hashCode method defined by
* class {@code Object} does return distinct integers for distinct
* objects. (This is typically implemented by converting the internal
* address of the object into an integer, but this implementation
* technique is not required by the
* Java™ programming language.)
....
*/
So, in essence, the numbers can not be used to determine if the content is equal. You could for example use: Arrays.equals().
In essence, the contents are the same (the arrays are). You are however making the mistake of checking something that is object instance specific.
Your code does however still have one mistake:
String thisByte = "".format("%x", a[i]);
Instead of %x use %02x, this ensures that the output is at least 2 digits (and when its a byte value also a max length of 2). (Also String.format(..) is a bit more accepted than "".format(..))