I want to convert the below String hex to an Int (16).
a6 5f de 57
What have I done, but I don't know it's the right way and I can't verify if the number is correct or not.
byte[] rec = new byte[20];
DatagramPacket pRec = new DatagramPacket(rec, rec.length);
socket.receive(pRec);
String tData = "";
for(int i = 0; i < 20; i++){
tData += String.format("%02x", rec[i]);
}
The output from the packet:
ffffffff730aa65fde5700000000000000000000
And now i must skip the first 6 bytes [ffffffff730a] and I need the remaining 4 bytes [a65fde57]. Note: [00000000000000000000] are NULL because the length of the buffer.
System.out.println( hexToInt(tData, 6, 10) );
private Integer hexToInt(String str, int start, int end){
String t = str.substring(start, end);
char[] ch = t.toCharArray();
String res = "";
for(int i = 0; i < end-start; i += 2){
res += Integer.parseInt(ch[i]+ch[i+1]+"", 16);
}
return Integer.parseInt(res);
}
And the result is:
516262
Is this the right way to convert a hex String to an int? Is the result are correct?
DatagramPacket). See Little Endian and Big Endian.