Implementing Checksum Using Java


Following is the code to implement Checksum using Java −

Example

 Live Demo

import java.util.*;
public class Demo{
   public static void main(String args[]){
      Scanner my_scan = new Scanner(System.in);
      System.out.println("Enter the input string ");
      String my_in = my_scan.next();
      int my_checksum = generate_checksum(my_in);
      System.out.println("The checksum that has been generated is " + Integer.toHexString(my_checksum));
      System.out.println("Enter the data that needs to be sent to the receiver ");
      my_in = my_scan.next();
      System.out.println("Enter the checksum that needs to be sent to the receiver ");
      my_checksum = Integer.parseInt((my_scan.next()), 16);
      receive(my_in, my_checksum);
      my_scan.close();
   }
   static int generate_checksum(String s){
      String my_hex_val = new String();
      int x, i, my_checksum = 0;
      for (i = 0; i < s.length() - 2; i = i + 2){
         x = (int) (s.charAt(i));
         my_hex_val = Integer.toHexString(x);
         x = (int) (s.charAt(i + 1));
         my_hex_val = my_hex_val + Integer.toHexString(x);
         System.out.println(s.charAt(i) + "" + s.charAt(i + 1) + " : " + my_hex_val);
         x = Integer.parseInt(my_hex_val, 16);
         my_checksum += x;
      }
      if (s.length() % 2 == 0){
         x = (int) (s.charAt(i));
         my_hex_val = Integer.toHexString(x);
         x = (int) (s.charAt(i + 1));
         my_hex_val = my_hex_val + Integer.toHexString(x);
         System.out.println(s.charAt(i) + "" + s.charAt(i + 1) + " : "+ my_hex_val);
         x = Integer.parseInt(my_hex_val, 16);
      } else {
         x = (int) (s.charAt(i));
         my_hex_val = "00" + Integer.toHexString(x);
         x = Integer.parseInt(my_hex_val, 16);
         System.out.println(s.charAt(i) + " : " + my_hex_val);
      }
      my_checksum += x;
      my_hex_val = Integer.toHexString(my_checksum);
      if (my_hex_val.length() > 4){
         int carry = Integer.parseInt(("" + my_hex_val.charAt(0)), 16);
         my_hex_val = my_hex_val.substring(1, 5);
         my_checksum = Integer.parseInt(my_hex_val, 16);
         my_checksum += carry;
      }
      my_checksum = generate_complement(my_checksum);
      return my_checksum;
   }
   static void receive(String s, int my_checksum){
      int gen_checksum = generate_checksum(s);
      gen_checksum = generate_complement(gen_checksum);
      int syndrome = gen_checksum + my_checksum;
      syndrome = generate_complement(syndrome);
      System.out.println("The value of syndrome is " + Integer.toHexString(syndrome));
      if (syndrome == 0){
         System.out.println("Data has been received without any errors");
      } else {
         System.out.println("An error was encountered in the received data");
      }
   }
   static int generate_complement(int my_checksum){
      my_checksum = Integer.parseInt("FFFF", 16) - my_checksum;
      return my_checksum;
   }
}

Inputs

sample
sample
b2c8

Output

Enter the input string
sa : 7361
mp : 6d70
le : 6c65
The checksum that has been generated is b2c8
Enter the data that needs to be sent to the receiver
Enter the checksum that needs to be sent to the receiver
sa : 7361
mp : 6d70
le : 6c65
The value of syndrome is 0
Data has been received without any errors

A class named Demo contains the main function. Here, a Scanner instance is defined and an input string is defined.

The ‘generate_checksum’ function is defined which creates a new string instance and initializes the checksum to 0. The string passed to this function as parameter is iterated over, and converted to hexadecimal value. Every character is iterated over and converted to integer value. It is then converted to a hexadecimal value. Then, the hexadecimal value is added to the next character’s hexadecimal value.

If the length of the string is even, every character is iterated over and converted to integer value. It is then converted to a hexadecimal value. Then, the hexadecimal value is added to the next character’s hexadecimal value. Otherwise, it is concatenated with ‘00’.

The ‘receive’ functions calls the ‘generate_checksum’ function and passes the result. Then it is added to the original checksum value (that was initially 0). Then it is converted to hexadecimal value. If this length is greater than 4, then a ‘carry’ value is generated. The original checksum is parsed and added to the ‘carry’ value. Once this snippet executes, the ‘generate_complement’ function is called on the checksum value. The ‘generate_complement’ function subtracts the checksum value from ‘FFFF’-16.

Updated on: 04-Jul-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements