Implementation of CRC cyclic redundancy check using java - Code

Latest

programs

Tuesday, October 11, 2011

Implementation of CRC cyclic redundancy check using java


Implementation of CRC cyclic redundancy check using java

Implementation of CRC cyclic redundancy check using java

/**
 *
 */
package com.gnec;

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

/**
 * @author Administrator
 *
 */
public class Crc8 {



            public static void main(String[] args)throws Exception {
                        // TODO Auto-generated method stub
                        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                       
                        //transmitted data
                        System.out.println("HOW MANY NUMBER OF BITS YOU WANT TO TRANSMIT: ");
                        int n=Integer.parseInt(br.readLine());
                        System.out.println("Enter transmitted data in binary form: ");
                        int td[]=new int[n];
                        for(int i=0;i<n;i++)
                                    td[i]=Integer.parseInt(br.readLine());
                                   
                        //generator data
                        System.out.println("Enter CRC8 polynomial generator data in binary form: ");       
                        int g[]=new int[9];
                        for(int i=0;i<9;i++)
                                    g[i]=Integer.parseInt(br.readLine());
                       
                        int l=9,m;
                        int stemp[]=null;
                        //append zeros
                        int atd[]=new int[n+8];
                        for(int i=0;i<n;i++)
                                    atd[i]=td[i];
                        for(int i=n;i<n+8;i++)
                                    atd[i]=0;
                        System.out.print("Transmitted data after append in binary form: ");
                        for(int i=0;i<atd.length;i++)
                                    System.out.print(atd[i]);
                       
                        //copy atd[] data into temp[] for modification
                        int temp[]=new int[atd.length];
                        System.out.println();
                        System.out.println("the length of the atd is:"+atd.length );
                        System.out.print("the temp data is:");
                        for(int i=0;i<atd.length;i++)
                        {
                        temp[i]=atd[i];
                        System.out.print(temp[i]);
                        }
                        m=atd.length;
                        System.out.println("\nthe l value is :"+l+"\t the m value is:"+m+"\t temp value:"+temp.length);
                        while(l<m)
                        {         
                        /*         for(int i=0;i<atd.length;i++)
                                    {
                                    temp[i]=atd[i];
                                    //System.out.print(temp[i]);
                                    }
                        */         int k=0;
                        System.out.println("previous data:");
                                    for(int i=0;i<l;i++)
                                    {
                                    temp[i]=temp[i]^g[i];
                                    k++;
                                    System.out.print(temp[i]);
                                    }
                                   
                                   
                                    /*for(int i=0;i<temp.length;i++)
                                                System.out.print("the temp data in while loop:"+temp[i]);
                                                */
                                    System.out.println("remaining of data:");
                                    for(int i=k;i<m;i++)
                                    {
                                                temp[i]=temp[i];
                                                System.out.print(temp[i]);
                                    }
                                               
                                    int count=0;
                                    for(int i=0;i<temp.length;i++)
                                    {          if(temp[i]==0)
                                                            count++;
                                                else
                                                            break;
                                    }
                                   
                                    stemp=new int[m-count];
                                    int j=0;
                                    System.out.println("some inter of data:");
                                    for(int i=count;i<temp.length;i++)
                                    {
                                                stemp[j]=temp[i];
                                               
                                                System.out.print(stemp[j]);
                                                j++;
                                    }
                                   
                                    temp=new int[j];
                                    for(int i=0;i<temp.length;i++)
                                    {
                                                temp[i]=stemp[i];
                                    }
                                    m=j;
                                                                                   
                        }
                                   
                        /*
                        int ttemp[]=new int[l-1];
                        if(temp.length<(l-1))
                        {          int x=0,j;
                                    int k=(l-1)-temp.length;
                                    //ttemp[]=new int[l-1];
                                    for(int i=0;i<k;i++)
                                                ttemp[i]=0;
                                    j=ttemp.length;
                                   
                                    for(int i=j;i<(l-1);i++)
                                    {
                                                ttemp[j]=temp[x];
                                                x++;
                                    }                                             
                        }
                        else if(temp.length==(l-1))
                        {
                                    //int ttemp[]=new int[l-1];
                                    for(int i=0;i<temp.length;i++)
                                    {
                                                ttemp[i]=temp[i];
                                    }
                        }
                        else
                                    System.out.println("some error has been occured");
                        */
                        System.out.print("The CRC is:");
                        for(int i=0;i<stemp.length;i++)
                                    System.out.print(stemp[i]);
                       
            }

}

Expected Output:

HOW MANY NUMBERS OF BITS YOU WANT TO TRANSMIT:   14
Enter transmitted data in binary form: 1 1 0 0 1 1 0 0 0 1 1 1 0 0
Enter CRC8 polynomial generator data in binary form:1 0 0 0 0 0 1 1 1
Transmitted data after append in binary form: 1 1 0 0 1 1 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0
The CRC is: 0 1 0 0 1 0 0 1 0

No comments:

Post a Comment