Sunday, September 19, 2010

CRC code goes here !!!

Hey guys this is my CRC code !!!

will upload whatever possible !!!

import java.io.*;

class CRC
{
    public static void main(String[] args)
    throws IOException
    {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        StringBuffer input,tans;
        String temp;
        int l,n,i,j;
        System.out.println("Enter input String");
        temp = br.readLine();
        input = new StringBuffer(temp);
        System.out.println("Enter Disivor");
        String divisor = br.readLine();
        l=temp.length(); n=divisor.length();
        char z[] = new char[n];
        for(j=0;j<n;j++)
        z[j]='0';
        String zeros = new String(z);
        tans=new StringBuffer(input.substring(0,n));
        for(i=0;i<n-1;i++)
        {
            input.append('0');
        }

        System.out.println("=====================\n"+input+"\n"+tans);
        for(i=0;i<l;i++)
        {
            if(tans.charAt(0)=='1')
            {
                System.out.println("------------");
                System.out.println(tans);
                System.out.println(divisor);
                tans=ExOr(tans,divisor);
            }
            else
            {
                System.out.println("------------");
                System.out.println(tans);
                System.out.println(zeros);
                tans=ExOr(tans,zeros);
            }
            tans=new StringBuffer(tans.substring(1,n));    //discard MSB
            if(i!=l-1)
            tans.append(input.charAt(i+n));            //append LSB
        }

        System.out.println("------------\nremainder --> "+tans);

        System.out.println("------------\nCodeWord -->"+input.substring(0,l)+tans);

    }

    public static StringBuffer ExOr(StringBuffer tans1,String divisor1)
    {
        StringBuffer ans=new StringBuffer();
        for(int m=0;m<divisor1.length();m++)
        {
            ans.append(switchoperation(tans1.charAt(m),divisor1.charAt(m)));
        }
        return ans;
    }
    public static char switchoperation(char t1,char t2)
    {
        if(t1==t2)
            return '0';
        else
            return '1';
    }
}
/*OUTPUT
Enter input String
11010110
Enter Disivor
10101
=====================
110101100000
11010
------------
11010
10101
------------
11111
10101
------------
10101
10101
------------
00000
00000
------------
00000
00000
------------
00000
00000
------------
00000
00000
------------
00000
00000
------------
remainder --> 0000
------------
CodeWord -->110101100000
Press any key to continue . . .*/

No comments: