Thursday April 18, 2024

Java XOR Checksum Calculator

Java Program

You can see where computing XORs manually would take quite a bit of time. We can rewrite this logic in Java and save many hours of simple, but tedious calculations.

   import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class hexcksum {

public static void main(String[] args) {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the Hexadecimal numbers:");
String str="0";
try {
str = bf.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String stringArray[] = str.split(" ",0);
System.out.println(stringArray.length);

String binary1 = "00000000";
String binary2 = "00000000";
String xorValue = "00000000";
for(int i = 0; i < stringArray.length; i++){
binary2 = Integer.toBinaryString(Integer.parseInt(stringArray[i].trim(),16));
xorValue = xor(binary1,binary2);
binary1 = xorValue;
}

System.out.println(xorValue);
int i= Integer.parseInt(xorValue,2);
String hexString = Integer.toHexString(i);
System.out.println(hexString);
}

public static String xor ( String binary1, String binary2 )
{
if (binary1.length() < 8) {
for (int n=binary1.length(); n<8; n++){
binary1 = "0" + binary1;
}
}
if (binary2.length() < 8) {
System.out.println(binary2.length());
for (int n=binary2.length(); n<8; n++){
binary2 = "0" + binary2;
}
}

char[] binary1Array = binary1.toCharArray();
char[] binary2Array = binary2.toCharArray();

String result = "";
int xorResult;
for (int i=0; i<8; i++){
xorResult = (binary1Array[i] ^ binary2Array[i]);
result = result.concat(Integer.toString(xorResult));
}

System.out.println (binary1);
System.out.println (binary2);
System.out.println (result + "**");
return result;
}
}




Output of Java Program:

   Enter the Hexadecimal numbers:
87 A5 03 00 40 00 A7 03 30 00 00 B0 01 10 B4 03 00 01 00 A4 02 00 01 23

00000000
10000111
10000111**
10000111
10100101
00100010**
00100010
00000011
00100001**
00100001
00000000
00100001**
00100001
01000000
01100001**
01100001
00000000
01100001**
01100001
10100111
11000110**
11000110
00000011
11000101**
11000101
00110000
11110101**
11110101
00000000
11110101**
11110101
00000000
11110101**
11110101
10110000
01000101**
01000101
00000001
01000100**
01000100
00010000
01010100**
01010100
10110100
11100000**
11100000
00000011
11100011**
11100011
00000000
11100011**
11100011
00000001
11100010**
11100010
00000000
11100010**
11100010
10100100
01000110**
01000110
00000010
01000100**
01000100
00000000
01000100**
01000100
00000001
01000101**
01000101
00100011
01100110**
01100110
66

So, in the output above you'll see two binary strings followed by a binary string with two astericks (**) after it. The binary string with astericks is the XOR result of the previous two lines. The final output of the program would be hex "66" which is the summed XOR of all the previous hex bytes in the string "87 A5 03 00 40 00 A7 03 30 00 00 B0 01 10 B4 03 00 01 00 A4 02 00 01 23". I'm not going to call it the most elegant Java code but it does the job just fine.

page1 page2


Comments:
Best on the web
Posted 04/18/13 8:33AM by Anonymous Techdoser
Super!
Love the code and explanation.
Re: Hi Im getting 66 as result for the same input as above?
Posted 03/12/13 7:21AM by AceBHound
Thanks to both of you pointing this out, I must have missed copying the "23" hex value with the hex string. The correct hex value should be 0x66 (ie. 66) with the hex 0x23 included in the input string.
Re: Hi Im getting 66 as result for the same input as above?
Posted 03/12/13 2:15AM by Anonymous Techdoser
i was getting 66 too
but when selecting input as only
"87 A5 03 00 40 00 A7 03 30 00 00 B0 01 10 B4 03 00 01 00 A4 02 00 01" excluding 23 (which is the length of the hex tokens) the result is 45.
CSafe Code Sample
Posted 09/27/12 12:50PM by fabiana
Does anybody has a basic CSafe Code in Java or C/C++/C# ? Thanks!!!
Im getting 66 as result
Posted 12/14/11 4:18AM by gk_sezhian
Hi Im getting 66 as result for the same input as above?
Re: Implémentation of CSafe protocol
Posted 09/24/11 11:49AM by AceBHound
Sorry, I don't have any more information on implementing the CSAFE protocol at this time. The fitlinxx.com/CSAFE describes the protocol and should help from a coding perspective, but it doesn't have any code examples. You would need to implement the hardware
Implémentation of CSafe protocol
Posted 09/19/11 10:10AM by delleyma
Hello,
thanks for your code!

I'm actually working in a project where I have to implement CSafe protocol .
I can't find how to do it.
Could help me on this ?

Thanks a lot !
Thanks a lot!
Posted 03/14/10 2:12PM by sdime
Many thanks to author! This code helped me to calculate xor checksum for my GPS tracker device!