Checksum
• In checksum error detection scheme, the data is divided into k segments each of m bits.
• In the sender’s end the segments are added using 1’s complement arithmetic to get the sum. The sum is complemented to get the checksum.
• The checksum segment is sent along with the data segments.
• At the receiver’s end, all received segments are added using 1’s complement
arithmetic to get the sum. The sum is complemented.
• If the result is zero, the received data is accepted; otherwise discarded.
CODE
import java.util.*;
public class Checksum
{
public static void main(String[] args)
{
System.out.println("Enter 16 bits Data :");
Scanner sc= new Scanner(System.in);
String s=sc.nextLine();
String senddata=send(s);
System.out.println("Send Data :"+ senddata);
receive(senddata);
}
public static String send(String s)
{
String s1=s.substring(0,8);
String s2=s.substring(8);
String total=add(s1,s2);
String checksum=getcomplement(total);
System.out.println("CheckSum :"+checksum);
return s+checksum;
}
public static void receive(String s)
{
String s1=s.substring(0,8);
String s2=s.substring(8,16);
String s3=s.substring(16);
String sum=add(s1,s2);
sum=add(s3,sum);
String complement=getcomplement(sum);
System.out.println("receiver sum :"+sum);
System.out.println("receiver sum complement :"+complement);
if(complement.equals("00000000"))
System.out.println("data receive correctly");
else
System.out.println("data receive wrong");
}
public static String add(String s1, String s2)
{
String sum="";
int carry=0;
String[] str1=s1.split("");
String[] str2=s2.split("");
for(int i=str1.length-1;i>=0;i--)
{
int a=Integer.parseInt(str1[i]);
int b=Integer.parseInt(str2[i]);
int total=a^b^carry;
carry=(a&b)|(b&carry)|(carry&a);
sum=String.valueOf(total)+sum;
}
if(carry==1)
{
sum=add(sum ,"00000001");
}
return sum;
}
public static String getcomplement(String s)
{
s=s.replaceAll("1","X");
s=s.replaceAll("0","1");
s=s.replaceAll("X","0");
return s;
}
}
OUTPUT
Enter 16 bits Data :
1111000011110000
CheckSum :00011110
Send Data :111100001111000000011110
receiver sum :11111111
receiver sum complement :00000000
data receive correctly
Post a Comment
0 Comments