Java gets the CRC8 check code for the string (modified by the Code of the C program in order to Java code)

Source: Internet
Author: User

CRC8 algorithm Please Baidu, I do not understand, here just to run their own successful structure posted out. The method Crc8_tab is not handled here because it is not used in my program.

Package Com.crc;public class Ccrc8_3 {/*public static int[] Crc8_tab = {0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12,0x15, 0x 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D, 0x70, 0x77,0x7e, 0x79, 0x6c, 0x6b, 0x62, 0x65, 0x48, 0x4f, 0x46, 0x41, 0x54, 0x53, 0x5A, 0x5d, 0xE0, 0xE7, 0xEE, 0xe9, 0xFC, 0xFB, 0xF2, 0xf5,0xd8, 0xDF, 0xd6, 0xD1, 0xc4, 0xC3, 0xCA, 0xCD, 0x90, 0x9 7, 0x9e,0x99, 0x8c, 0x8b, 0x82, 0x85, 0xa8, 0xAF, 0xa6, 0xa1, 0xb4, 0xb3,0xba, 0xBD, 0xC7, 0xC0, 0xc9, 0xCE, 0xDB, 0xDC, 0  XD5, 0xd2, 0xff,0xf8, 0xF1, 0xf6, 0xe3, 0xE4, 0xED, 0xEA, 0xb7, 0xb0, 0xb9, 0xbe,0xab, 0xAC, 0xa5, 0xa2, 0x8F, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9d,0x9a, 0x27, 0x20, 0x29, 0x2e, 0x3B, 0x3C, 0x35, 0x32, 0x1F, 0x18,0x11, 0x16, 0x03, 0x04, 0x0D, 0x0 A, 0x57, 0x50, 0x59, 0x5E, 0x4b,0x4c, 0x45, 0x42, 0x6F, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7d, 0x7a,0x89, 0x8E, 0x87, 0x80, 0  X95, 0x92, 0x9b, 0x9c, 0xb1, 0xb6, 0xbf,0xb8, 0xAD, 0xAA, 0xa3, 0xa4, 0xf9, 0xFE, 0xf7, 0xF0, 0xe5, 0xe2,0xeb, 0xEC, 0xC1, 0xC6, 0xCF, 0xC8, 0xDD, 0xDA, 0xd3, 0xd4, 0x69,0x6e, 0x67, 0x60, 0x75, 0x72, 0x7B, 0x7C, 0x51, 0x56, 0x5f, 0x58,0x4d, 0x4A, 0x43, 0x44, 0x19, 0x1 E, 0x17, 0x10, 0x05, 0x02, 0x0b,0x0c, 0x21, 0x26, 0x2F, 0x28, 0x3d, 0x3A, 0x33, 0x34, 0x4E, 0x49,0x40, 0x47, 0x52, 0x55, 0  X5C, 0x5b, 0x76, 0x71, 0x78, 0x7F, 0x6a,0x6d, 0x64, 0x63, 0x3e, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2C, 0x2b,0x06, 0x01, 0x08, 0x0F, 0x1A, 0x1D, 0x14, 0x13, 0xAE, 0xa9, 0xa0,0xa7, 0xb2, 0xb5, 0xBC, 0xBB, 0x96, 0x91, 0x98, 0x9F, 0x8A, 0x8d,0x84, 0x8 3, 0xDE, 0XD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB, 0xe6,0xe1, 0xe8, 0xEF, 0xFA, 0xFD, 0xf4, 0xf3};p ublic static int crc8_ Tab (int ucptr,int uclen) {int UCINDEX;//CRC8 check table index int ucCRC8 =0;//crc8 byte initialize//CRC8 bit check while (Uclen--! =0) {ucindex= uccrc8^ (ucptr++); uccrc8=crc8_tab[ucindex];} Returns the CRC8 checksum data               return (~ucCRC8 );} */public static int FINDCRC (byte[] data) {int Crc=0;int genpoly = 0xaa;for (int i=0;i<data.length; i++) {CRC ^= Data[i];foR (int j=0;j<8;j++) {if (CRC & 0x80)! = 0) {CRC = (CRC << 1) ^ genpoly;} ELSE{CRC <<= 1;}}} The CRC &= 0xff;//guarantees a CRC remainder code output of 2 bytes. return CRC;} public static void Main (string[] args) {String abc = "12431312SDRFD"; int crc2 = FINDCRC (Abc.getbytes ()); String CRC16 = integer.tohexstring (CRC2);//convert 10 binary results to 16//If you want to ensure that the check code must be 2 bits, you can first determine whether the result is smaller than 16, if it is smaller than 16, you can add 0if in front of the result of the 16 binary ( Crc2 <) {CRC16 = "0" +CRC16;} System.out.println (CRC16);}}

  

Java gets the CRC8 check code for the string (modified by the Code of the C program in order to Java code)

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.