A little comprehension of C # CRC check

Source: Internet
Author: User

Today, when I tinker with a handheld operator, I have a little problem and record today's experience pack

Because before the company's products in the calibration is basically and check, today in preparing to use C # to simulate an antique operator, but encountered a problem, the simulator issued data, the motherboard will not reply, compared to the communication protocol has not found any problems. Because the document is not complete, just know the communication format, compared to think it should be a check out the problem. Because CRC check is the most commonly used in the field of data communication verification method, asked a few old guys to know that this four-byte ASCII checksum should be Crc16-ccitt generated, and then go to the University of Idiot for a long time did not understand the details of CRC check.

How the specific CRC is generated I don't elaborate, the key point is "generate polynomial" and initial value.

The generative polynomial of Crc16-ccitt is 0x1021;

Not much to say, provide code Crc16-ccitt class

 Public classCrc16ccitt { Public enumInitialcrcvalue {zeros, NonZero1 =0xFFFF, NonZero2 =0x1d0f }        Const ushortPoly =4129; ushort[] table =New ushort[ the]; ushortInitialValue =0;  PublicCrc16ccitt (Initialcrcvalue initialvalue) { This. InitialValue = (ushort) InitialValue; ushorttemp, A;  for(inti =0; I < table. Length; ++i) {temp=0; A= (ushort) (I <<8);  for(intj =0; J <8; ++j) {if((temp ^ a) &0x8000) !=0) {Temp= (ushort) (Temp <<1) ^Poly); }                    Else{Temp<<=1; } A<<=1; } Table[i]=temp; }        }         Public ushortComputechecksum (byte[] bytes) {            ushortCRC = This. InitialValue;  for(inti =0; I < bytes. Length; ++i) {CRC= (ushort) (CRC <<8) ^ table[(CRC >>8) ^ (0xFF&bytes[i])); }            returnCRC; }         Public ushortComputechecksum (list<byte>listtemp) {            byte[] bytes =listtobytes (listtemp); ushortCRC = This. InitialValue;  for(inti =0; I < bytes. Length; ++i) {CRC= (ushort) (CRC <<8) ^ table[(CRC >>8) ^ (0xFF&bytes[i])); }            returnCRC; }         Public byte[] Computechecksumbytes (byte[] bytes) {            ushortCRC =computechecksum (bytes); returnBitconverter.getbytes (CRC); }         Public byte[] Listtobytes (list<byte>listtemp) {            intLength =Listtemp.count (); byte[] bytes =New byte[length];  for(inti =0; i < length; i++) {Bytes[i]=Listtemp[i]; }            returnbytes; }    }

Finally, please call me red scarf

A little comprehension of C # 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.