软件注册站
热情软件屋

 
CRC算法以及实现方法
编号: QA001309    
建立日期: 1999年7月8日 最后修改日期: 2003年11月4日
所属类别: Visual Basic - 网络与通信
   
    vb
    Windows98
    请详细介绍CRC算法,以及实现方法。
    (袁文彬)
   
    限于时间,我无法向你详细地介绍CRC的原理,如果你感兴趣可以看一些通信方面的书,比如曹志刚、钱亚生的《现代通信原理》,在这些书中都详细介绍了CRC的原理和计算方法。目前常用的CRC编码有CRC-12、CRC-16、CRC-CCITT、CRC-32等。
    如果你想得到C语言的实现,可以参考http://tvland.maxinet.com/relling/rae-crc1.htm
    如果你想得到Delphi语言实现,可以参考
    http://www.efg2.com/Lab/Mathematics/CRC.htm
    如果你想得到Visual Basic.NET的实现,可以参考《Visual Basic.NET解决方案工具箱--30个.NET实用组件》。该书的代码可以从下载http://support.apress.com/books.asp?bID=1861007396&s=0&Go=Select+Book
    下面是一个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
    
    fulium的意见:
    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

    

此问题由李海回答。

附加关键字:编程, 源程序, programming, source code, Visual Basic, VB, 网络与通信, network, communicate, com, com1, com2

   
 
把这个问题推荐给朋友
   
 
   
您的意见类别
您的名字
您的电子邮件
您的建议(请尽可能详细)
 
 

版权所有 1997-2008 热情软件屋
如果您有任何建议和意见, 请给我发个电子邮件 askpro@china-askpro.com
Web Designed by ZebraStudio