Delphi CRC16 Check Algorithm implementation

Source: Internet
Author: User
Tags 0xc0

Cyclic redundancy code Check English name is cyclical redundancy check, referred to as CRC. It uses the principle of division and remainder to make false detection (error detecting). In practical application, the transmitting device calculates the CRC value and sends it along with the data to the receiving device, the receiving device recalculates the CRC to the received data and compares with the received CRC, if the two CRC values are different, the data communication error is shown.

Depending on the application environment and habits, the CRC can be divided into the following criteria:

①CRC-12 Code;

②crc-16 Code;

③crc-ccitt Code;

④CRC-32 code.

CRC-12 codes are commonly used to transmit 6-bit strings. CRC-16 and Crc-ccitt codes are used to transmit 8-bit characters, of which CRC-16 is used in the United States, and Crc-ccitt is used by European countries. Most of the CRC-32 codes are used in a synchronous transmission called point-to-point.

The following emphasis is on the CRC-16 test code generation process.

The CRC-16 code consists of two bytes, at the beginning of each bit of the CRC register is preset to 1, and then the CRC register with the 8-bit data, and then the CRC registers from high to low shift, the highest bit (MSB) position 0, and the lowest bit (LSB, After the shift has been removed from the CRC register) if 1, the register is different from the predefined polynomial code or, if the LSB is zero, no XOR is required. Repeat the above from the high to the low shift 8 times, the first 8-bit data processing completed, with the CRC register value at this time and the next 8-bit data xor and carry out as the previous data 8 times shift. The value in the CRC register is the final CRC value after all character processing is complete.

The following is the calculation procedure for CRC16, where the resulting polynomial is: x16+x15+x2+1:

1. Set the CRC register and assign it a value of FFFF (hex).

2. The first 8-bit character of the data is different from the low 8 bits of the 16-bit CRC register, and the result is stored in the CRC register.

3. The CRC register moves one bit to the right, the MSB complements 0, and the LSB is removed and checked.

4. If the LSB is 0, repeat the third step, if the LSB is a 1,CRC register differs from the generated polynomial code or.

5. Repeat steps 3rd and 4th until all 8 shifts are complete. At this point a 8-bit data processing is complete.

6. Repeat steps 2nd through 5th until all data processing is complete.

7. The content of the final CRC register is the CRC value.


Verification Code Implementation

There are two ways to write a CRC check program: One is a computational method and one is a table-checking method. The following is the C language implementation of the table-checking method:


Verification Code Algorithm Program implementation example (C language):


Test.cpp:Defines the entry point for the console application.


#include "stdafx.h"


static unsigned char auchcrchi[]={
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
0X81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
0X81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
0X81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
0X81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40
};
static unsigned char auchcrclo[]={
0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06,
0x07, 0xC7, 0x05, 0xC5, 0xc4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD,
0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xc9, 0x09,
0x08, 0xC8, 0xd8, 0x18, 0x19, 0xd9, 0x1B, 0xDB, 0xDA, 0x1A,
0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xd4,
0xd5, 0x15, 0xD7, 0x17, 0x16, 0xd6, 0xd2, 0x12, 0x13, 0xd3,
0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xf3,
0xF2, 0x32, 0x36, 0xf6, 0xf7, 0x37, 0xf5, 0x35, 0x34, 0xf4,
0x3C, 0xFC, 0xFD, 0x3d, 0xFF, 0x3F, 0x3e, 0xFE, 0xFA, 0x3A,
0x3B, 0xFB, 0x39, 0xf9, 0xF8, 0x38, 0x28, 0xe8, 0xe9, 0x29,
0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2e, 0x2F, 0xEF, 0x2D, 0xED,
0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xe5, 0x27, 0xE7, 0xe6, 0x26,
0x22, 0xe2, 0xe3, 0x23, 0xe1, 0x21, 0x20, 0xE0, 0xA0, 0x60,
0x61, 0xa1, 0x63, 0xa3, 0xa2, 0x62, 0x66, 0xa6, 0xa7, 0x67,
0xa5, 0x65, 0x64, 0xa4, 0x6c, 0xAC, 0xAD, 0x6d, 0xAF, 0x6F,
0x6e, 0xAE, 0xAA, 0x6A, 0x6b, 0xAB, 0x69, 0xa9, 0xa8, 0x68,
0x78, 0xb8, 0xb9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E,
0x7F, 0xBF, 0x7d, 0xBD, 0xBC, 0x7C, 0xb4, 0x74, 0x75, 0xb5,
0x77, 0xb7, 0xb6, 0x76, 0x72, 0xb2, 0xb3, 0x73, 0xb1, 0x71,
0x70, 0xb0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92,
0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9c, 0x5C,
0X5D, 0x9d, 0x5f, 0x9F, 0x9E, 0x5E, 0x5A, 0x9a, 0x9b, 0x5b,
0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8b,
0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4f, 0x8d, 0x4d, 0x4C, 0x8c,
0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42,
0x43, 0x83, 0x41, 0x81, 0x80, 0x40
};


unsigned short CRC16 (unsigned char* puchmsg, unsigned short usdatalen)
{
unsigned char Uchcrchi = 0xFF;
unsigned char uchcrclo = 0xFF;
unsigned char uIndex;
while (usdatalen--)
{
UIndex = Uchcrchi ^ *puchmsg++;
Uchcrchi = Uchcrclo ^ Auchcrchi[uindex];
Uchcrclo = Auchcrclo[uindex];
}
Return (Uchcrchi << 8 | uchcrclo);
};


void Main (int argc, char* argv[])
{
unsigned char src[21]={' 1 ', ' 2 ', ' 3 ', ' 6 ', ' 3 ', ' 4 ', ' 2 ', ' 3 ', ' 4 ', ' 2 ', ' 9 ', ' 8 ', ' 7 ', ' 3 ', ' 6 ', ' 5 ', ' 2 ', ' 8 ', ' 9 '};
unsigned Short code = CRC16 (src,21);
printf ("The result is%d/n", code);
}

The following is the Delphi implementation of the Code


Unit CRC16;

Interface
Uses
Windows, Sysutils;

function CRC (a_strpuchmsg:string): Longword;

Implementation

Const
CT_ARRAYCRCHI:ARRAY[0..255] of longword=
(
$00, $C 1, $81, $01, $C 0, $, $41, $01, $C 0,
$41, $00, $C 1, $81, $ $01, $C 0, $, $41,
$00, $C 1, $81, $00, $C 1, $81, $ $C, $01, 0,
$ $41, $01, $C 0, $ $41, $00, $C 1, $81, $ A,
$00, $C 1, $81, $01, $C 0, $, $41, $00, $C 1,
$81, $01, $C 0, $41, $01, $C 0, $, $41,
$00, $C 1, $81, $01, $C 0, $, $41, $00, $C 1,
$81, $00, $C 1, $81, $ $01, $C 0, $, $41,
$00, $C 1, $81, $01, $C 0, $, $41, $01, $C 0,
$ $41, $00, $C 1, $81, $ $00, $C 1, $81, $ A,
$01, $C 0, $41, $01, $C 0, $, $41, $00, $C 1,
$81, $ $01, $C 0, $ $41, $00, $C 1, $81, $ A,
$00, $C 1, $81, $01, $C 0, $, $41, $01, $C 0,
$ $41, $00, $C 1, $81, $ $00, $C 1, $81, $ A,
$01, $C 0, $41, $00, $C 1, $81,, $01, $C 0,
$ $41, $01, $C 0, $ $41, $00, $C 1, $81, $ A,
$00, $C 1, $81, $01, $C 0, $, $41, $01, $C 0,
$41, $00, $C 1, $81, $ $01, $C 0, $, $41,
$00, $C 1, $81, $00, $C 1, $81, $ $C, $01, 0,
$41, $00, $C 1, $81, $ $01, $C 0, $, $41,
$01, $C 0, $41, $00, $C 1, $81,, $01, $C 0,
$ $41, $00, $C 1, $81, $ $00, $C 1, $81, $ A,
$01, $C 0, $41, $01, $C 0, $, $41, $00, $C 1,
$81, $00, $C 1, $81, $ $01, $C 0, $, $41,
$00, $C 1, $81, $01, $C 0, $, $41, $01, $C 0,
$ $41, $00, $C 1, $81, $
);
CT_ARRAYCRCLO:ARRAY[0..255] of longword=
(
$00, $C 0, $C 1, $01, $C 3, $03, $02, $C 2, $C 6, $06,
$07, $C 7, $05, $C 5, $C 4, $04, $CC, $0c, $0d, $CD,
$0f, $CF, $CE, $0e, $0a, $CA, $CB, $0b, $C 9, $09,
$08, $C 8, $D 8, $18, $19, $D 9, $1b, $DB, $DA, $1A,
$1E, $DE, $DF, $1f, $DD, $1d, $1c, $DC, $14, $D 4,
$D 5, $ A, $D 7, $17, $16, $D 6, $D 2,, $13, $D 3,
$11, $D 1, $D 0, $ A, $F 0, $31, $F 1, $33, $F 3,
$F 2, $32, $36, $F 6, $F 7, $37, $F 5, $, $34, $F 4,
$3C, $FC, $FD, $3d, $FF, $3f, $3e, $FE, $FA, $3a,
$3b, $FB, $F 9, $F 8, $38, $28, $E 8, $E 9, $29,
$EB, $2b, $2a, $EA, $EE, $2e, $2f, $EF, $2d, $ED,
$EC, $2C, $E 4, $24, $ $E, 5, $27, $E 7, $E 6, $26,
$22, $E 2, $E 3, $23, $E 1, $21, $, $E 0, $A 0, $,
$61, $A 1, $63, $A 3, $A 2, $62, $66, $A 6, $A 7, $67,
$A 5, $65, $64, $A 4, $6c, $AC, $AD, $6d, $AF, $6f,
$6e, $AE, $AA, $6a, $6b, $AB, $69, $A 9, $A 8, $68,
$78, $B 8, $B 9, $79, $BB, $7b, $7a, $BA, $BE, $7e,
$7f, $BF, $7d, $BD, $BC, $7c, $B 4, $74, $ $, $B 5,
$77, $B 7, $B 6, $76, $72, $B 2, $B 3, $73, $B 1, $71,
$ $B 0, $ $91, $51, $93, $53, $52, $92,
$96, $56, $57, $97, $55, $95, $94, $54, $9c, $5c,
$5D, $9d, $5f, $9f, $9e, $5e, $5a, $9a, $9b, $5b,
$59, $58, $98, $88, $48, $49, $89, $4b, $8b,
$8a, $4a, $4e, $8e, $8f, $4f, $8d, $4d, $4c, $8c,
$44, $84, $85, $45, $87, $47, $46, $86, $82, $42,
$43, $83, $41, $81, $, $
);

function CRC (a_strpuchmsg:string): Longword;
Var
L_crchi, L_crclo, L_index, L_datalen, I:longword;
Begin
l_datalen:= Length (a_strpuchmsg);
L_crchi: = $FF;
L_crclo: = $FF;
L_index: = 0;
I:=1;
while (I<=l_datalen) do
Begin
L_index: = L_crchi xor Integer (A_strpuchmsg[i]);
L_crchi: = L_crclo xor Ct_arraycrchi[l_index];
L_crclo: = Ct_arraycrclo[l_index];
INC (i);
End
result:= (L_crchi SHL 8) or L_crclo;
End

End.

Delphi CRC16 Check algorithm implementation (GO)

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.