Implementing CRC16 parity code with Java

Source: Internet
Author: User
Tags array length stringbuffer

1. Design

2. Code

import java.io.IOException;


public class Crc16checker {


private static int[] index = new int[] {16, 15, 2, 0};


private static int[] Getbinary (String text) {


StringBuffer num = new StringBuffer ();


String S; Char ch;


for (int i = 0; i < text.length (); i++) {//change per char to binary code.


s = integer.tobinarystring (Text.charat (i));


//If The code is less than 8 bit and make it as 8 bit.


for (int j = 8-s.length (); j > 0; j--) num.append (0);


Num.append (s);


             }


int len = Num.length ();


Int[] Code = new Int[len];


for (int i = 0; i < len; i++)//change per 0/1 char to int.


Code[i] = Character.getnumericvalue (Num.charat (i));


return code;


       }


private static String Tohex (int[] num) {


stringbuffer hex = new StringBuffer (NUM.LENGTH/4);


char[] ch = new CHAR[4];


for (int i = 0; i < num.length;) {


//change all 0/1 int to char.


ch[0] = Character.fordigit (num[i++], 2);


ch[1] = Character.fordigit (num[i++], 2);


ch[2] = Character.fordigit (num[i++], 2);


ch[3] = Character.fordigit (num[i++], 2);


//change per 4-bit-code to hex number.


hex.append (integer.tohexstring (Integer.parseint (string.valueof (CH), 2));


             }


return hex.tostring ();


       }


//CRC codes main process


public static int[] Makecrccodes (int[] sourcecodes, int[] multinomial) {


//The lenght of CRC code is N bits longer than source code. The Codes


//From 0 to Sourcelength are same as the source. N bits after source


//are the CRC codes. N is decided by the multinomial.


//CRC code array length of the original code length plus check code length. The front of the array stores the original code. Check code stored in array


//last n bit. The length of the checksum code is determined by the generation of elements on the 0 position of the polynomial array.


int sourcelength = sourcecodes.length;


int codeslength = sourcelength + multinomial[0];


int[] crccodes = new Int[codeslength];


Copy source code from 0 to Sourcelength. Copy the original code.


system.arraycopy (sourcecodes, 0, crccodes, 0, sourcelength);


int temp, POS;


//Division system. Divider.


for (int i = 0; i < sourcelength; i++) {


//Count value of the input adding.


//With the first and second register value modulo two plus.


temp = (Crccodes[sourcelength] + sourcecodes[i])% 2;


//Move registers forwards from (1, length) to (0, length-1).

The
//second register and all subsequent register values move forward 1 digits.


System.arraycopy (


Crccodes, Sourcelength + 1, crccodes, Sourcelength, multinomial[0]-1);


//Set The last register with counted value.


The last register value holds the calculated input value.


crccodes[codeslength-1] = temp;


//Count other registers. The position is calculated by the value of the generated polynomial, and modulo two adds the value of the register.


for (int j = Index.length-2 J > 0; j--) {


pos = codeslength-multinomial[j]-1;


Crccodes[pos] = (Crccodes[pos] + temp)% 2;


                   }


             }


return crccodes;


       }


public static void Main (string[] args) throws IOException {


System.out.print ("Input hex Data:");


StringBuffer buf = new StringBuffer ();


char ch = (char) System.in.read ();


while (ch!= ' \ R ' && ch!= ' \ n ') {


buf.append (CH);


ch = (char) System.in.read ();


             }


//Get binary codes.


int[] B = crc16checker.getbinary (buf.tostring ());


//Make CRC codes.


B = crc16checker.makecrccodes (b, Crc16checker.index);


//Output code as binary number.


for (int i = 0; i < b.length;) {


for (int j = 0; J < 4; J + +, i++) System.out.print (b[i));


System.out.print (");


             }


System.out.println ();


//Output code as hex number.


System.out.println ("The CRC16 Code is:" + Crc16checker.tohex (b));


       }


}

3. Test:

Case : A (ASCII = 41)
Result : 410186
CRC bits : 0168 (0000 0001 1000 0110)
Run program :
Input hex data :A
0100 0001 0000 0001 1000 0110
The CRC16 code is :410186
Case : CRC16TEST (ASCII = 43 52 43 31 36 54 45 53 54)
Result : 455243313654455354fb66
CRC bits : fb66 (1111 1011 0110 0110)
Run program :
Input hex data :CRC16TEST
0100 0011 0101 0010 0100 0011 0011 0001 0011 0110 0101 0100 0100 0101 0101 0011 0101 0100 1111 1011 0110 0110
The CRC16 code is :435243313654455354fb66
Case : 5k (ASCII = 35 6b)
Result : 356b3f79
CRC bits : 3f79 (0011 1111 0111 1001)
Run program :
Input hex data :5k
0011 0101 0110 1011 0011 1111 0111 1001
The CRC16 code is :356b3f79

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.