CRC check algorithm and C # program to realize __ algorithm

Source: Internet
Author: User

The CRC checksum can be used to validate the data during transmission, when sending valid data, the CRC check code is computed according to the valid data and generating polynomial (such as CCITT Standard polynomial is x16+x12+x5+1), and the CRC check code is added to the valid data to send; when receiving data, Take out the previous valid data section, use the same generation polynomial to calculate the CRC check code, and then remove the received data after the CRC Check code section, compare two check code is the same. If the same, the data received is the same as the data sent, the transmission is correct, if different, the transmission data is considered an error.

CRC (cyclic redundancy check) algorithm is mainly a process of computing division. The algorithm has two input values, the first is the input signal, which is usually a very long data, as dividend. The second is a polynomial related to the specific CRC algorithm, called a generating polynomial, used as a divisor. The basic calculation process is that the two do modulo 2 division (essentially the corresponding bit to do XOR or operation), the remainder is the result of CRC check code.

I, the basic algorithm (artificial written calculation):
Taking Crc16-ccitt as an example, the generation polynomial of X16+X12+X5+1,CRC is 16 bits, and 17 bits of polynomial are generated. If the data flow is 4 bytes: byte[3], byte[2], byte[1], byte[0];
Data flow left 16-bit, the equivalent of expanding 256x256 times, divided by the generation of polynomial 0x11021, do not borrow division operations (equivalent to bitwise XOR), the remainder is the CRC check code.
The data stream is 6 bytes when sent: Byte[3], byte[2], byte[1], byte[0, crc[1], crc[0];
II, computer algorithm 1 (bit-type algorithm):
1 the enlarged data stream (6 bytes) high 16 bits (byte[3], byte[2]) into a register of length 16;
2 If the first register is 1, the register is shifted to the left 1 bits (the lowest bit of the register is obtained from the next byte), and then the denoted of the generating polynomial is different or;
Otherwise, only the register is moved to the left 1 bits (the lowest bit of the register is obtained from the next byte);
3 Repeat the 2nd step until the data stream (6 bytes) is moved into the register;
4) The value of the register is CRC check code crc[1], crc[0].
III, Computer algorithm 2 (byte-type algorithm):
The general description of the byte-type algorithm is: The CRC code of this byte, equal to the lower 8-bit left 8-bit of the last byte CRC code, and the last byte CRC is shifted to the right 8-bit to the CRC code that is different from or after the same byte.
The byte-type algorithm is as follows:
1 The CRC Register Group is initialized to all "0" (0x0000). (Note: CRC Register group initialization is all 1 o'clock, the final CRC should be reversed.) )
2 The CRC Register Group is shifted to the left 8 bits and is saved to the CRC Register Group.
3 The original CRC register group high 8 bits (8-bit right) and the data byte of the difference or operation, to get a point to the Value table index.
4 The index refers to the table value and CRC register group to do different or operations.
5 The data pointer plus 1, repeat step 2 if the data is not fully processed.
6) to obtain CRC.

Simple Example

The following is a simple example to illustrate the CRC algorithm calculation process. The input signal is 101111, and the generated polynomial is 1001 (the corresponding mathematical expression is x3+1). Dividend need to add 3 0. 101111000 to 1001 to do modulo 2 division operation, to get a 3-bit remainder of 010, this is the CRC check code. In the example above, the remainder is 010. Append the remainder to the input signal, that is, send the data to 101111010, take the first six bits of the data received by the receiving end to 1001 modulo 2 division operation, to see if the CRC parity code is equal to receive the data after three digits. If yes, the transmission is correct, if not, transmission error.

4 C # program code

This is a program written using the bit-type algorithm.

The GETCRC method in the following Crcverifylhy class is used to compute the CRC code, in the main function The byte array is used to hold the data to be transferred (note: The first two bytes are initially 0, used to hold the CRC result, the actual data to be transferred from the third byte). Here simply transfer a byte of data, such as the following 157, you can manually use written calculation out a CRC check code, and then see the program output is the same as the manual, by my verification, this algorithm program should be correct.

Using System;

Using System.Collections.Generic;

Using System.Text;

Namespace Crcverify

{

Class Crcverifylhy

{

The datastream[0] and datastream[1 in the datastream array are the initial values of the CRC code, that is, the 0x0000. The other array element is the information code to be transmitted, crc_16 is the denoted of the generating polynomial

Taking Crc16-ccitt as an example, the CRC check code is 16 bits, 17 bits of polynomial are generated, and the denoted type is actually 0x11021,

But the highest bit of a polynomial is fixed at 1, so the denoted is ignored in the highest bit 1, and the Crc16-ccitt denoted can be written as 0x1021

public static ushort GETCRC (byte[] dataStream, ushort crc_16)

{

ushort Crc_temp = convert.touint16 ((datastream[datastream.length-1) << 8) + datastream[datastream.length-2]);

int totalbit = (datastream.length-2) * 8;

for (int i = totalBit-1 i >= 0; i--)

{

UShort A = convert.touint16 (I/8);

ushort B = convert.touint16 (i% 8);

ushort Nextbit = convert.touint16 ((datastream[a) >> b) & 0x01);

if (crc_temp >= 32768)

{

Crc_temp = Convert.touint16 ((crc_temp-32768) << 1) + nextbit);

Crc_temp = convert.touint16 (crc_temp ^ crc_16);

}

Else

{

Crc_temp = convert.touint16 (crc_temp << 1) + nextbit);

}

}

return crc_temp;

}

}

Class Program

{

static void Main (string[] args)

{

byte[] array = new byte[] {0, 0, 157};

ushort Crc_result = CRCVERIFYLHY.GETCRC (array, 0x1021);

Console.WriteLine (Crc_result);

Console.readkey ();

}

}

}

The following recommendations are used:

Using System;

Namespace USTC
{
<summary>
Message CRC CHECKSUM algorithm
</summary>
public class CRC
{
private int key = 0x11021h;
Public CRC ()
{

}
public static int Getkey (byte[] data)
{
int count = data. Length;
byte[] buf = new Byte[data. Length + 2];
Data. CopyTo (buf, 0);
int ptr = 0;
int i = 0;
int CRC = 0;
Byte Crc1, CRC2, CRC3;
CRC1 = buf[ptr++];
CRC2 = buf[ptr++];
Buf[count] = 0;
Buf[count + 1] = 0;
while (--count >= 0)
{
CRC3 = buf[ptr++];
for (i = 0; i < 8; i++)
{
if ((Crc1 & 0x80) >> 7 = = 1)//Determine whether the Crc1 high is 1
{
Crc1 = (byte) (Crc1 << 1); Move out of position
if ((Crc2 & 0x80) >> 7 = = 1)//Determine whether the Crc2 high is 1
{
Crc1 = (byte) (Crc1 | 0x01)//crc1 low from 0 to 1
}
Crc2 = (byte) (CRC2 << 1);//crc2 move out of position
if ((CRC3 & 0x80) >> 7 = = 1)//judge whether the CRC3 high is 1
{
Crc2 = (byte) (Crc2 | 0x01); Crc2 low from 0 to 1
}
CRC3 = (byte) (CRC3 << 1);//crc3 move out of position
Crc1 = (byte) (crc1 ^ 0x10);
Crc2 = (byte) (crc2 ^ 0x21);
}
Else
{
Crc1 = (byte) (Crc1 << 1); Move out of position
if ((Crc2 & 0x80) >> 7 = = 1)//Determine whether the Crc2 high is 1
{
Crc1 = (byte) (Crc1 | 0x01)//crc1 low from 0 to 1
}
Crc2 = (byte) (CRC2 << 1);//crc2 move out of position
if ((CRC3 & 0x80) >> 7 = = 1)//judge whether the CRC3 high is 1
{
Crc2 = (byte) (Crc2 | 0x01); Crc2 low from 0 to 1
}
CRC3 = (byte) (CRC3 << 1);//crc3 move out of position
}
}
}
CRC = (int) ((Crc1 << 8) + CRC2);
return CRC;
}
}
}

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.