Saturday July 31, 2010

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
45

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 "45" 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". I'm not going to call it the most elegant Java code but it does the job just fine.

page1 page2


Comments:
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!



If you have an account, please login below. You can sign up for a new account, it's free!
You can also post anonymously as Anonymous Techdoser if you leave the username/password fields blank.

*Username
*Password
Subject
Comment  
 




To verify you're a human please type the letters in the image above


*Due to URL spamming, comments will be approved before being posted.




Partners
Sony LCD TV
Read Reviews On Sony LCD TV's
Buy Desktop, Laptop & Netbook Computers online at Big Brown Box
Dish Network HD Programming
Quality resolution with HD from Dish.