📄 crc算法以及实现方法.txt
字号:
目 前 常 用 的 CRC编 码 有 CRC-12、 CRC-16、 CRC-CCITT、 CRC-32等 。
下 面 是 一 个 CRC-32和 CRC-16的 VB实 现 。
'Header taken from original C source code:
'/* CRC-32 version 1.04 by Craig Bruce, 05-Dec-1994
'**
'** Based on "File Verification Using CRC" by Mark R. Nelson in Dr. Dobb's
'** Journal, May 1992, pp. 64-67. This program DOES generate the same CRC
'** values as ZMODEM and PKZIP
'**
'** v1.00: original release.
'** v1.01: fixed printf formats.
'** v1.02: fixed something else.
'** v1.03: replaced CRC constant table by generator function.
'** v1.04: reformatted code, made ANSI C. 05-Dec-1994.
'*/
'
'Translated to (Visual) Basic Darren Kelly, 01-Jul-1999
'Lines with comments have been added to make the crc code work
'02-Jul-1999 CRC Table generation works, compared against c code
' v1.00: Direct translation, modified to VB
' v1.01: Divided into Table and CRC generation procedures, incorporated as module CRC
' Developed in conjunction with <snipped> project
' v1.02: Added support for skipping the last (SkipEnd) bytes
Private CrcTable(256)
Dim CRC As Long, ToGet As Long
Private poly As Long, i As Long, j As Long, mask As Long, TempIntx As
Integer
Private InByte As Byte, dbl As Double
Sub CRCGenerateTable()
'generate crc table first
poly = &HEDB88320 'pre-calculated polynomial used for CCITT CRC-32 calculation
For i = 0 To 256
CRC = i
For j = 1 To 8
mask = CRC And &H80000000 'if there is a 1 in highest bit...
If (CRC And 1) = 1 Then
CRC = Int(CRC / 2) Xor poly ' Without Int() returns one
higher.
Else
CRC = (CRC / 2)
End If
CRC = CRC Xor mask ' ... then remove it
Next j
CrcTable(i) = CRC
Next i
End Sub
Function CRCof(ByVal FileNum, Optional ByVal SkipBeginning As Long,
Optional ByVal SkipEnd As Long) As Long 'Pass File Number, recieve decimal. Good to convert using Hex$(CRCof)
'Add: optional skip bytes at start, skip bytes at end, for files with additional
'Headers and... um, footers?
'On Error Resume Next
dbl = &HFFFFFFFF 'Have to use a double-precision number because basic supports not an unsigned long type.
'set up file buffer
ToGet = LOF(1) - Loc(1) - SkipEnd
While EOF(FileNum) = False And ToGet > 0 'also use counter to skip last SkipEnd bytes
Get #FileNum, , InByte ' next version, get using file buffer
i = (dbl Xor InByte) And &HFF
j = CrcTable(i)
If dbl < 0 Then dbl = Int(dbl / 256) Else dbl = Fix(dbl / 256)
'division here requires double. also int rounds negative numbers down, fix rounds up
dbl = (dbl And &HFFFFFF) Xor j
TempIntx = TempIntx + 1
If TempIntx > 10000 Then
TempInt = DoEvents()
TempIntx = 0
End If
ToGet = ToGet - 1
'if buffer is empty, refill
Wend
'final result
CRCof = dbl Xor &HFFFFFFFF
'Debug.Print Hex(CRC)
End Function
Function ComputeCRC(ByRef inBuf() As Byte, ByVal nStart As Long,
ByVal nBytes As Long) As Long
'*****************************************************
' Purpose: Calculates the 16-bit CCITT CRC check of
' a sequence of bytes
' Inputs: inBuf() input array of bytes
' nStart starting point in inBuf
' nBytes number of bytes to use
' Returns: The unsigned 16-bit CRC as a long
' Method: Devised by Andy Lowry, Columbia University
' The magic number &H1081 is derived from
' the CRC-CCITT polynomial x^16+x^12+x^5+1
'*****************************************************
Dim crc As Long 'running CRC value
Dim q As Long 'intermediate value
Dim c As Long 'byte being used, as a long
Dim i As Long 'loop counter
crc = 0
For i = nStart To nStart + nBytes - 1
c = inBuf(i)
q = (crc Xor c) And &HF& 'low nibble
crc = (crc \ &H10&) Xor (q * &H1081&)
q = (crc Xor (c \ &H10&)) And &HF& 'high nibble
crc = (crc \ &H10&) Xor (q * &H1081&)
Next i
ComputeCRC = crc
End Function
如 果 你 想 得 到 C语 言 的 实 现 , 可 以 参 考 http://tvland.maxinet.com/relling/rae-crc1.htm。
<END>
CRC-CCITT:
R(x)=(X^16(X^15+X^14+ ... +1) + X^16*G(x))/P(x)
P(x)=X^16+X^12+X^5+1
<END>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -