Java version CRC check

Source: Internet
Author: User

<span style= "White-space:pre" ></span>/** * calculates the generated check code * * @param data * needs to be validated * @return */public St atic String MAKE_CRC (byte[] data) {byte[] buf = new byte[data.length];//stores the data for which the checksum needs to be generated for (int i = 0; i < data.length; I + +) {Buf[i] = data[i];} int len = buf.length;int CRC = 0xffff;for (int pos = 0; pos < len; pos++) {if (Buf[pos] < 0) {CRC ^= (int) Buf[pos] + 256;  XOR byte into least sig. Byte of//CRC} else {CRC ^= (int.) Buf[pos];//XOR byte into least sig. Byte of crc}for (int i = 8; I! = 0; i--) {//Loop over each bitif ((CRC & 0x0001)! = 0) {//If the LSB is SETCRC >>= 1;//Shift Right and XOR 0xA 001CRC ^= 0xa001;} else//Else LSB is not SETCRC >>= 1;  Just shift right}}string C = integer.tohexstring (CRC); if (c.length () = = 4) {c = c.substring (2, 4) + c.substring (0, 2);} else if (c.length () = = 3) {c = "0" + c;c = c.substring (2, 4) + c.substring (0, 2);} else if (c.length () = = 2) {c = "0" + C . substring (1, 2) + "0" + c.substring (0,1);} return c;}

Java version CRC check

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.