⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.java

📁 CRC via table lookup
💻 JAVA
字号:
/* Programmed By: Dhaval Tamboli                  2493194         Program: Program to compute the CRC of a message using CRC-16*/package crcmain;import java.io.*;public class Main {     int crc16(char[] message)    {        int i,j,bytes,crc,mask;        int[] table = new int[256];        if(table[1]==0)        {            for(bytes=0;bytes<=255;bytes++)            {                crc = bytes;                for(j=7;j>=0;j--)                {                    mask = -(crc & 1);                    crc = (crc >> 1) ^ (0xA001 & mask);                }                table[bytes] = crc;            }        }        i=0;        crc = 0xFFFF;        while (message[i] != 0)        {            bytes = message[i];            crc = (crc >> 8) ^ table[(crc ^ bytes) & 0xFF];            i = i+1;        }        return crc;    }    //Function Which calculates CRC16.    int crc(char[] msg,int len)    {        int index;        int crc=0xFFFF,genpoly=0xA001;        int i=0,bytes;        while(msg[i]!=0)        {            bytes = msg[i];            crc = crc ^ bytes;            for(index=0;index<8;index++)            {               if((crc & 0x0001)==1) crc = (crc >> 1) ^ genpoly;               else crc =crc >> 1;            }            i++;        }        return crc;}    public static void main(String[] args) {        String record = "";        char[] array = new char[512];        int j=0;        int result;        if(args.length != 1)        {            System.out.println("Please Enter File name");            System.exit(1);        }        try        {            FileInputStream fis = new FileInputStream(args[0]);            BufferedInputStream bis = new BufferedInputStream(fis);            DataInputStream dis = new DataInputStream(bis);            //Reads Data from file and stores in to Character Array            while ( (record=dis.readLine()) != null )            {                int length = record.length();                for(int i=0;i<length;i++)                {                    array[j]=record.charAt(i);                    j++;                }           }           //Copy Character Array into another array for future purpose           char newArray[] = new char[j];           for(int i=0;i<j;i++)           {                newArray[i] = array[i];            }           //Calls the crc method and stores result in integer variable named 'result'           Main main = new Main();           result = main.crc(array, j);           //convert crc into Binary string           String res1 = Integer.toBinaryString(result);           int result1 = main.crc16(array);           String str1 = Integer.toHexString(result1);           //convert result into Hexadecimal string           String finalResult = Integer.toHexString(result);           System.out.println("CRC of Data (In HexaDecimal):"+finalResult+"\t via table:"+str1);           //Takes crc in reverse binary order and stores in temporary string           if(res1.length()!=16) res1=res1+'0';           String crc1 = res1.substring(8, 16);           String temp = res1.substring(0, 8);           String crc2="";                      for(int k=temp.length()-1;k>=0;k--)           {               crc2 = crc2 + temp.charAt(k);           }                    // convert binary string into ASCII character           byte crcByte1 = (byte) Integer.parseInt(crc1,2);           byte crcByte2 = (byte) Integer.parseInt(crc2,2);           //Make a new character array with the same file data           String str="";           for(int i=0;i<newArray.length;i++)           str = str + newArray[i];           finalResult = str;           //Appending CRC characters at the last of character array           finalResult = finalResult + (char)crcByte1;           finalResult = finalResult + (char)crcByte2;                      System.out.println("String after appending CRC: "+finalResult);           //Converting string into character array           char[] crcArray = new char[512];           for(int i=0;i<finalResult.length();i++)           {               crcArray[i]=finalResult.charAt(i);           }           //calling crc method again           result = main.crc(crcArray, j);           finalResult = Integer.toHexString(result);           System.out.println("CRC after appending CRC16 characters:"+finalResult+"  \t via table:"+str1);                   }catch(Exception e){System.out.println("Exception Cause: "+e.toString());}    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -