How to write CRC check program under VB

Source: Internet
Author: User
Tags final integer md5 md5 encryption
With the development of computer technology, the industrial control of data communication using microcomputer has been applied more and more widely in modern industry. The communication data between the computer and the controlled device often occurs unpredictable errors due to the transmission distance, the field condition and many other possible factors. In order to prevent the impact of errors, generally in the communication with the method of data validation, and cyclic redundancy code check is the most common method of calibration.

The principle of cyclic redundancy code checking

Cyclic redundancy Code Verification English name is cyclical redundancy Check, referred to CRC. It uses the principle of division and remainder to make error detection (Error detecting). In practical application, the sending device calculates the CRC value and sends it to the receiving device with the data, 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 indicated.

According to the application environment and habits of different, CRC can be divided into the following several criteria:

①CRC-12 Code;

②crc-16 Code;

③crc-ccitt Code;

④CRC-32 code.

CRC-12 code is commonly used to transmit 6-bit strings. CRC-16 and Crc-ccitt codes are used to transmit 8-bit characters, of which CRC-16 are used by the United States, while 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 is an example of the most commonly used CRC-16 to illustrate its build process.

The CRC-16 code is composed of two bytes, at the beginning, each of the CRC registers is preset to 1, then the CRC registers are 8-bit with the data, then the CRC registers are shifted from high to low, at the top (MSB) position to fill 0, and the lowest bit (LSB, After the shift has been moved out of the CRC register if 1, then the register with the predefined polynomial code, or if the LSB is zero, you do not need to be different or. Repeat the above shift from high to low 8 times, the first 8-bit data processing, with the CRC register with the value of the next 8-bit data or and carry out as the previous data like 8 times shift. The value in the CRC register is the final CRC value after all character processing is completed.

The following is the calculation procedure for CRC:

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 registers move one bit to the right, MSB 0, remove and check the LSB.

4. If the LSB is 0, repeat the third step, if the LSB is a 1,CRC register that is different from the polynomial code.

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

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

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

Second, the compilation of cyclic redundancy code check Program


Understand the CRC check code generation process, the program is very easy to write. Because of the widespread popularity of Visual Basic and its important position in the data communication, the following VB language to write the CRC generation program, the other language only need to do a little modification.

There are two ways to write a CRC calibration program: one for the calculation and one for the look-up table method. The following two methods are discussed separately.


1. Calculation method

The calculation method is to design the program according to the principle of CRC check code generation. The advantage is that the module code is less, the modification is flexible, the portability is good. The disadvantage is that the computational capacity is large. For the sake of understanding, three-bit data is assumed here, and the polynomial code is A001 (hex).

Place a command button Command1 on the form and add the following code:


Private Sub Command1_Click ()

Dim CRC () as Byte

Dim d () as Byte to transfer data

ReDim D (2) as Byte

D (0) = 123

D (1) = 112

D (2) = 135

CRC = CRC16 (d) ' Calls CRC16 calculation function

' CRC (0) is high

' CRC (1) is low

End Sub

Note: The CRC may be low on the data transfer, and the post is high.


Function CRC16 (data () as Byte) as String

Dim Crc16lo as Byte, Crc16hi as Byte CRC register

Dim CL as Byte, CH as byte polynomial code &ha001

Dim Savehi as Byte, Savelo as Byte

Dim I as Integer

Dim Flag as Integer

Crc16lo = &hff

Crc16hi = &hff

CL = &h1

CH = &ha0

For I = 0 to Ubound (data)

Crc16lo = Crc16lo Xor data (I) ' Each one is different from the CRC register or

For Flag = 0 to 7

Savehi = Crc16hi

Savelo = Crc16lo

Crc16hi = crc16hi \ 2 ' High position to move right one

Crc16lo = crc16lo \ 2 ' Low right shift one

if ((Savehi and &h1) = &h1) Then ' if the last digit of the high-order byte is 1

Crc16lo = Crc16lo Or &h80 ' The lower byte moves to the right and the front is 1

End If ' Otherwise it will automatically fill 0

if (Savelo and &h1) = &h1) Then ' If the LSB is 1, the polynomial code is different or

Crc16hi = Crc16hi Xor CH

Crc16lo = Crc16lo Xor CL

End If

Next Flag

Next I

Dim Returndata (1) as Byte

Returndata (0) = Crc16hi ' CRC high

Returndata (1) = Crc16lo ' CRC low

CRC16 = Returndata

End Function

2. Tabular method

The advantages and disadvantages of the tabular method are the exact opposite of the computational method. For ease of comparison, all of the assumptions here are identical to the calculation, and a Command1 button is placed on the form, and the Code section is exactly the same as the above. The following only describes the CRC function to write the source code.


Private Function CRC16 (data () as Byte) as String

Dim Crc16hi as Byte

Dim Crc16lo as Byte

Crc16hi = &hff

Crc16lo = &hff

Dim I as Integer

Dim Iindex as Long

For I = 0 to Ubound (data)

Iindex = Crc16lo Xor data (I)

Crc16lo = Crc16hi Xor getcrclo (iindex) ' low-level processing

Crc16hi = Getcrchi (iindex) ' High-level processing

Next I

Dim Returndata (1) as Byte

Returndata (0) = Crc16hi ' CRC high

Returndata (1) = Crc16lo ' CRC low

CRC16 = Returndata

End Function


' CRC low byte value table

Function Getcrclo (Ind as Long) as Byte

   Getcrclo = Choose (Ind + 1, &h0, &HC1, &h81, &h40, &h1, &hc0, &h80, &h41, &H1, & HC0, &h80, &h41, &h0, &HC1, &h81, &h40, &h1, &hc0, &h80, &h41, &H0, &HC1, &h81, &h40, &h0, &HC1, &h81, &h40, &h1, &hc0, &h80, &h41, &H1, &HC0, & H80, &h41, &h0, &HC1, &h81, &h40, &h0, &HC1, &h81, &h40, &h1, &HC0, &H80, &h41, &h0, &HC1, &h81, &h40, &h1, &hc0, &h80, &h41, &h1, &HC0, &H80, & H41, &h0, &HC1, &h81, &h40, &h1, &hc0, &h80, &h41, &h0, &HC1, &H81, &H40, &h0, &HC1, &h81, &h40, &h1, &hc0, &h80, &h41, &h0, &HC1, &H81, &H40, & H1, &hc0, &h80, &h41, &h1, &hc0, &h80, &h41, &h0, &HC1, &h81, &H40, &H0, & Amp HC1, &h81, &H40, &h1, &hc0, &h80, &h41, &h1, &hc0, &h80, &h41, &h0, &HC1, &H81, &H40, &H1, &hc0, &h80, &h41, &h0, &HC1, &h81, &h40, &h0, &HC1, &H81, &H40, & H1, &hc0, &h80, &h41, &h1, &hc0, _

&h80, &h41, &h0, &HC1, &h81, &h40, &h0, &HC1, &h81, &h40, &H1, &HC0, & H80, &h41, &h0, &HC1, &h81, &h40, &h1, &hc0, &h80, &h41, &h1, &HC0, &H80, &h41, &h0, &HC1, &h81, &h40, &h0, &HC1, &h81, &h40, &h1, &HC0, &H80, & H41, &h1, &hc0, &h80, &h41, &h0, &HC1, &h81, &h40, &h1, &hc0, &H80, &H41, &h0, &HC1, &h81, &h40, &h0, &HC1, &h81, &h40, &h1, &hc0, &H80, &H41, & H0, &HC1, &h81, &h40, &h1, &hc0, &h80, &h41, &h1, &hc0, &h80, &H41, &H0, & Amp HC1, &h81, &h40, &h1, &hc0, &h80, &h41, &h0, &HC1, &h81, &h40, &H0, &HC1, &h81, &h40, &h1, &hc0, &h80, &h41, &h1, &hc0, &h80, &h41, &H0, &HC1, & H81, &h40, &H0, &HC1, &h81, &h40, &h1, &hc0, &h80, &h41, &h0, &HC1, &h81, &H40, &H1, & Amp HC0, &h80, &h41, &h1, &hc0, &h80, &h41, &h0, &HC1, &h81

End Function


' CRC high byte value table

Function Getcrchi (Ind as Long) as Byte

   Getcrchi = Choose (Ind + 1, &h0, &hc0, &HC1, &h1, &HC3, &h3, &h2, &HC2, &HC6, &H 6, &h7, &hc7, &h5, &hc5, &HC4, &h4, &HCC, &HC, &HD, &HCD, &HF, &HCF, & HCE, &he, &ha, &HCA, &HCB, &HB, &hc9, &h9, &h8, &hc8, &hd8, &H18, &H19, &A mp HD9, &H1B, &hdb, &had, &h1a, &h1e, &hde, &HDF, &h1f, &HDD, &h1d, &H1C, &HD C, &h14, &hd4, &hd5, &h15, &HD7, &H17, &h16, &hd6, &HD2, &h12, &H13, &HD3, &h11, &HD1, &hd0, &h10, &hf0, &h30, &h31, &hf1, &h33, &hf3, &HF2, &H32, & Amp H36, &hf6, &hf7, &h37, &hf5, &h35, &h34, &hf4, &H3C, &HFC, &HFD, &H3D, &HF F, &h3f, &h3e, &hfe, &HFA, &h3a, &h3b, &HFB, &h39, &hf9, &hf8, &H38, &H28, &he8, &hE9, &h29, &heb, &H2B, &H2A, &hea, &hee, &h2e, &h2f, &hef, &h2d, &HED, &HEC , &H2C, &he4, &h24, &h25, &he5, &h27, &he7, &he6, &h26, &h22, &HE2, &HE3, &h23, &he1, &h21, &h20, &he0, &HA0, &h60, _

&h61, &HA1, &h63, &ha3, &HA2, &h62, &h66, &ha6, &ha7, &h67, &HA5, &H65, &A mp H64, &HA4, &h6c, &hac, &had, &h6d, &haf, &h6f, &h6e, &hae, &haa, &H6A, &H6 B, &hab, &h69, &HA9, &ha8, &h68, &h78, &hb8, &hb9, &h79, &HBB, &H7B, &H7A, &hba, &hbe, &h7e, &h7f, &HBF, &h7d, &HBD, &HBC, &h7c, &HB4, &H74, &H75, & Amp HB5, &h77, &hb7, &hb6, &h76, &h72, &HB2, &HB3, &h73, &HB1, &h71, &H70, &HB 0, &h50, &H90, &h91, &h51, &h93, &h53, &h52, &h92, &h96, &h56, &H57, &H97, &h55, &h95, &h94, &h54, &h9c, &h5c, &h5d, &h9d, &h5f, &h9f, &H9E, &H5E, & Amp H5A, &h9a, &h9b, &h5b, &h99, &h59, &h58, &h98, &h88, &h48, &H49, &H89, &H4 B, &h8b,&H8A, &h4a, &h4e, &h8e, &h8f, &h4f, &h8d, &h4d, &h4c, &h8c, &H44, &H84, &A mp H85, &h45, &h87, &h47, &h46, &h86, &h82, &h42, &h43, &h83, &h41, &H81, &H8 0, &h40)

End Function


The above program is debugged and passed under WIN98,VB6.



Author blog:http://blog.csdn.net/huitiansou/
Related article string converted to hexadecimal
How to write CRC check program under VB
The realization of MD5 encryption algorithm in VB
MD5 Algorithm Implementation
Research on MD5 algorithm


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.