VB如何编写CRC-ITU,(内有C算法)
小弟是菜鸟,工作中需要软件进行CRC校验,给出的是C语言的算法,但是小弟不是很懂,网络上找出的CRC算法都不能得出正确的值,下面就是C语言的算法,请高人帮忙进行帮助,指导一下static const U16 crctab16[] =
{
0X0000, 0X1189, 0X2312, 0X329B, 0X4624, 0X57AD, 0X6536, 0X74BF,
0X8C48, 0X9DC1, 0XAF5A, 0XBED3, 0XCA6C, 0XDBE5, 0XE97E, 0XF8F7,
0X1081, 0X0108, 0X3393, 0X221A, 0X56A5, 0X472C, 0X75B7, 0X643E,
0X9CC9, 0X8D40, 0XBFDB, 0XAE52, 0XDAED, 0XCB64, 0XF9FF, 0XE876,
0X2102, 0X308B, 0X0210, 0X1399, 0X6726, 0X76AF, 0X4434, 0X55BD,
0XAD4A, 0XBCC3, 0X8E58, 0X9FD1, 0XEB6E, 0XFAE7, 0XC87C, 0XD9F5,
0X3183, 0X200A, 0X1291, 0X0318, 0X77A7, 0X662E, 0X54B5, 0X453C,
0XBDCB, 0XAC42, 0X9ED9, 0X8F50, 0XFBEF, 0XEA66, 0XD8FD, 0XC974,
0X4204, 0X538D, 0X6116, 0X709F, 0X0420, 0X15A9, 0X2732, 0X36BB,
0XCE4C, 0XDFC5, 0XED5E, 0XFCD7, 0X8868, 0X99E1, 0XAB7A, 0XBAF3,
0X5285, 0X430C, 0X7197, 0X601E, 0X14A1, 0X0528, 0X37B3, 0X263A,
0XDECD, 0XCF44, 0XFDDF, 0XEC56, 0X98E9, 0X8960, 0XBBFB, 0XAA72,
0X6306, 0X728F, 0X4014, 0X519D, 0X2522, 0X34AB, 0X0630, 0X17B9,
0XEF4E, 0XFEC7, 0XCC5C, 0XDDD5, 0XA96A, 0XB8E3, 0X8A78, 0X9BF1,
0X7387, 0X620E, 0X5095, 0X411C, 0X35A3, 0X242A, 0X16B1, 0X0738,
0XFFCF, 0XEE46, 0XDCDD, 0XCD54, 0XB9EB, 0XA862, 0X9AF9, 0X8B70,
0X8408, 0X9581, 0XA71A, 0XB693, 0XC22C, 0XD3A5, 0XE13E, 0XF0B7,
0X0840, 0X19C9, 0X2B52, 0X3ADB, 0X4E64, 0X5FED, 0X6D76, 0X7CFF,
0X9489, 0X8500, 0XB79B, 0XA612, 0XD2AD, 0XC324, 0XF1BF, 0XE036,
0X18C1, 0X0948, 0X3BD3, 0X2A5A, 0X5EE5, 0X4F6C, 0X7DF7, 0X6C7E,
0XA50A, 0XB483, 0X8618, 0X9791, 0XE32E, 0XF2A7, 0XC03C, 0XD1B5,
0X2942, 0X38CB, 0X0A50, 0X1BD9, 0X6F66, 0X7EEF, 0X4C74, 0X5DFD,
0XB58B, 0XA402, 0X9699, 0X8710, 0XF3AF, 0XE226, 0XD0BD, 0XC134,
0X39C3, 0X284A, 0X1AD1, 0X0B58, 0X7FE7, 0X6E6E, 0X5CF5, 0X4D7C,
0XC60C, 0XD785, 0XE51E, 0XF497, 0X8028, 0X91A1, 0XA33A, 0XB2B3,
0X4A44, 0X5BCD, 0X6956, 0X78DF, 0X0C60, 0X1DE9, 0X2F72, 0X3EFB,
0XD68D, 0XC704, 0XF59F, 0XE416, 0X90A9, 0X8120, 0XB3BB, 0XA232,
0X5AC5, 0X4B4C, 0X79D7, 0X685E, 0X1CE1, 0X0D68, 0X3FF3, 0X2E7A,
0XE70E, 0XF687, 0XC41C, 0XD595, 0XA12A, 0XB0A3, 0X8238, 0X93B1,
0X6B46, 0X7ACF, 0X4854, 0X59DD, 0X2D62, 0X3CEB, 0X0E70, 0X1FF9,
0XF78F, 0XE606, 0XD49D, 0XC514, 0XB1AB, 0XA022, 0X92B9, 0X8330,
0X7BC7, 0X6A4E, 0X58D5, 0X495C, 0X3DE3, 0X2C6A, 0X1EF1, 0X0F78,
};
// 计算给定长度数据的 16 位 CRC。
U16 GetCrc16(const U8* pData, int nLength)
{
U16 fcs = 0xffff; // 初始化
while(nLength>0){
fcs = (fcs >> 8) ^ crctab16[(fcs ^ *pData) & 0xff];
nLength--;
pData++;
}
return ~fcs; // 取反
}
VB C 算法 --------------------编程问答-------------------- 更改了一下,单个字节计算正常,但是多字节计算时还是有问题,哪位大大帮我看一下
'CRC-ITU查找表
Public Function GetCRCIntU(Ind As Integer) As Integer
GetCRCIntU = Choose(Ind + 1, _
&H0, &H1189, &H2312, &H329B, &H4624, &H57AD, &H6536, &H74BF, &H8C48, &H9DC1, &HAF5A, &HBED3, _
&HCA6C, &HDBE5, &HE97E, &HF8F7, &H1081, &H108, &H3393, &H221A, &H56A5, &H472C, &H75B7, &H643E, _
&H9CC9, &H8D40, &HBFDB, &HAE52, &HDAED, &HCB64, &HF9FF, &HE876, &H2102, &H308B, &H210, &H1399, _
&H6726, &H76AF, &H4434, &H55BD, &HAD4A, &HBCC3, &H8E58, &H9FD1, &HEB6E, &HFAE7, &HC87C, &HD9F5, _
&H3183, &H200A, &H1291, &H318, &H77A7, &H662E, &H54B5, &H453C, &HBDCB, &HAC42, &H9ED9, &H8F50, _
&HFBEF, &HEA66, &HD8FD, &HC974, &H4204, &H538D, &H6116, &H709F, &H420, &H15A9, &H2732, _
&H36BB, &HCE4C, &HDFC5, &HED5E, &HFCD7, &H8868, &H99E1, &HAB7A, &HBAF3, &H5285, &H430C, &H7197, _
&H601E, &H14A1, &H528, &H37B3, &H263A, &HDECD, &HCF44, &HFDDF, &HEC56, &H98E9, &H8960, &HBBFB, _
&HAA72, &H6306, &H728F, &H4014, &H519D, &H2522, &H34AB, &H630, &H17B9, &HEF4E, &HFEC7, &HCC5C, _
&HDDD5, &HA96A, &HB8E3, &H8A78, &H9BF1, &H7387, &H620E, &H5095, &H411C, &H35A3, &H242A, &H16B1, _
&H738, &HFFCF, &HEE46, &HDCDD, &HCD54, &HB9EB, &HA862, &H9AF9, &H8B70, &H8408, &H9581, _
&HA71A, &HB693, &HC22C, &HD3A5, &HE13E, &HF0B7, &H840, &H19C9, &H2B52, &H3ADB, &H4E64, &H5FED, _
&H6D76, &H7CFF, &H9489, &H8500, &HB79B, &HA612, &HD2AD, &HC324, &HF1BF, &HE036, &H18C1, &H948, _
&H3BD3, &H2A5A, &H5EE5, &H4F6C, &H7DF7, &H6C7E, &HA50A, &HB483, &H8618, &H9791, &HE32E, &HF2A7, _
&HC03C, &HD1B5, &H2942, &H38CB, &HA50, &H1BD9, &H6F66, &H7EEF, &H4C74, &H5DFD, &HB58B, &HA402, _
&H9699, &H8710, &HF3AF, &HE226, &HD0BD, &HC134, &H39C3, &H284A, &H1AD1, &HB58, &H7FE7, _
&H6E6E, &H5CF5, &H4D7C, &HC60C, &HD785, &HE51E, &HF497, &H8028, &H91A1, &HA33A, &HB2B3, &H4A44, _
&H5BCD, &H6956, &H78DF, &HC60, &H1DE9, &H2F72, &H3EFB, &HD68D, &HC704, &HF59F, &HE416, &H90A9, _
&H8120, &HB3BB, &HA232, &H5AC5, &H4B4C, &H79D7, &H685E, &H1CE1, &HD68, &H3FF3, &H2E7A, &HE70E, _
&HF687, &HC41C, &HD595, &HA12A, &HB0A3, &H8238, &H93B1, &H6B46, &H7ACF, &H4854, &H59DD, &H2D62, _
&H3CEB, &HE70, &H1FF9, &HF78F, &HE606, &HD49D, &HC514, &HB1AB, &HA022, &H92B9, &H8330, &H7BC7, _
&H6A4E, &H58D5, &H495C, &H3DE3, &H2C6A, &H1EF1, &HF78)
End Function
Public Function IsCrc16Good(pSource() As Byte, Optional iLen As Long = 1) As Boolean
Dim i As Integer
Dim crc(1) As Byte
Dim result As Integer
Dim tmp1 As Integer
Dim tmp2 As Integer
Dim L As Long
'dim H as
result = &HFFFF
' fcs = (fcs >> 8) ^ crctab16[(fcs ^ *pData) & 0xff];
If iLen = 1 Then iLen = UBound(pSource) + 1
iLen = iLen - 1
For i = 0 To iLen ' UBound(pSource)
Debug.Print pSource(i) & " ," & i
tmp1 = result Xor pSource(i)
tmp2 = tmp1 And &HFF
tmp1 = GetCRCIntU(tmp2)
tmp2 = (result And &HFF00) / 256 And &HFF
result = tmp2 Xor tmp1
Debug.Print result
'L = result
'result = "&H" & Right(Hex(result), 4)
' pSource = pSource(i)
'result = ((result And &HFF00) / 256 And &HFF) Xor GetCRCIntU(((result Xor pSource(i)) And &HFF))
Next
result = Not (result) --------------------编程问答-------------------- 你的算法对不对啊?
我按你的算法转,验算不对
form代码
ption Explicit
Dim s As String
Private Sub Command1_Click()
'static const U16 crctab16[] =
'{
' 0000, 1189, 2312, 329B, 4624, 57AD, 6536, 74BF,
' 8C48, 9DC1, AF5A, BED3, CA6C, DBE5, E97E, F8F7,
' 1081, 0108, 3393, 221A, 56A5, 472C, 75B7, 643E,
' 9CC9, 8D40, BFDB, AE52, DAED, CB64, F9FF, E876,
' 2102, 308B, 0210, 1399, 6726, 76AF, 4434, 55BD,
' AD4A, BCC3, 8E58, 9FD1, EB6E, FAE7, C87C, D9F5,
' 3183, 200A, 1291, 0318, 77A7, 662E, 54B5, 453C,
' BDCB, AC42, 9ED9, 8F50, FBEF, EA66, D8FD, C974,
' 4204, 538D, 6116, 709F, 0420, 15A9, 2732, 36BB,
' CE4C, DFC5, ED5E, FCD7, 8868, 99E1, AB7A, BAF3,
' 5285, 430C, 7197, 601E, 14A1, 0528, 37B3, 263A,
' DECD, CF44, FDDF, EC56, 98E9, 8960, BBFB, AA72,
' 6306, 728F, 4014, 519D, 2522, 34AB, 0630, 17B9,
' EF4E, FEC7, CC5C, DDD5, A96A, B8E3, 8A78, 9BF1,
' 7387, 620E, 5095, 411C, 35A3, 242A, 16B1, 0738,
' FFCF, EE46, DCDD, CD54, B9EB, A862, 9AF9, 8B70,
' 8408, 9581, A71A, B693, C22C, D3A5, E13E, F0B7,
' 0840, 19C9, 2B52, 3ADB, 4E64, 5FED, 6D76, 7CFF,
' 9489, 8500, B79B, A612, D2AD, C324, F1BF, E036,
' 18C1, 0948, 3BD3, 2A5A, 5EE5, 4F6C, 7DF7, 6C7E,
' A50A, B483, 8618, 9791, E32E, F2A7, C03C, D1B5,
' 2942, 38CB, 0A50, 1BD9, 6F66, 7EEF, 4C74, 5DFD,
' B58B, A402, 9699, 8710, F3AF, E226, D0BD, C134,
' 39C3, 284A, 1AD1, 0B58, 7FE7, 6E6E, 5CF5, 4D7C,
' C60C, D785, E51E, F497, 8028, 91A1, A33A, B2B3,
' 4A44, 5BCD, 6956, 78DF, 0C60, 1DE9, 2F72, 3EFB,
' D68D, C704, F59F, E416, 90A9, 8120, B3BB, A232,
' 5AC5, 4B4C, 79D7, 685E, 1CE1, 0D68, 3FF3, 2E7A,
' E70E, F687, C41C, D595, A12A, B0A3, 8238, 93B1,
' 6B46, 7ACF, 4854, 59DD, 2D62, 3CEB, 0E70, 1FF9,
' F78F, E606, D49D, C514, B1AB, A022, 92B9, 8330,
' 7BC7, 6A4E, 58D5, 495C, 3DE3, 2C6A, 1EF1, 0F78,
'};
'
'// 计算给定长度数据的 16 位 CRC。
'U16 GetCrc16(const U8* pData, int nLength)
'{
' U16 fcs = 0xffff; // 初始化
' while(nLength>0){
' fcs = (fcs >> 8) ^ crctab16[(fcs ^ *pData) & 0xff];
' nLength--;
' pData++;
' }
' return ~fcs; // 取反
'}
Dim A(7) As Byte
Dim i As Long
For i = 0 To 5
A(i) = 255 * Rnd
Next
End Sub
Private Sub Form_Load()
Dim s As String
Dim Items() As String
Dim n As Long, i As Long
s = "0000, 1189, 2312, 329B, 4624, 57AD, 6536, 74BF,8C48, 9DC1, AF5A, BED3, CA6C, DBE5, E97E, F8F7," & _
"1081, 0108, 3393, 221A, 56A5, 472C, 75B7, 643E,9CC9, 8D40, BFDB, AE52, DAED, CB64, F9FF, E876," & _
"2102, 308B, 0210, 1399, 6726, 76AF, 4434, 55BD,AD4A, BCC3, 8E58, 9FD1, EB6E, FAE7, C87C, D9F5," & _
"3183, 200A, 1291, 0318, 77A7, 662E, 54B5, 453C,BDCB, AC42, 9ED9, 8F50, FBEF, EA66, D8FD, C974," & _
"4204, 538D, 6116, 709F, 0420, 15A9, 2732, 36BB,CE4C, DFC5, ED5E, FCD7, 8868, 99E1, AB7A, BAF3," & _
"5285, 430C, 7197, 601E, 14A1, 0528, 37B3, 263A,DECD, CF44, FDDF, EC56, 98E9, 8960, BBFB, AA72," & _
"6306, 728F, 4014, 519D, 2522, 34AB, 0630, 17B9,EF4E, FEC7, CC5C, DDD5, A96A, B8E3, 8A78, 9BF1," & _
"7387, 620E, 5095, 411C, 35A3, 242A, 16B1, 0738,FFCF, EE46, DCDD, CD54, B9EB, A862, 9AF9, 8B70," & _
"8408, 9581, A71A, B693, C22C, D3A5, E13E, F0B7,0840, 19C9, 2B52, 3ADB, 4E64, 5FED, 6D76, 7CFF," & _
"9489, 8500, B79B, A612, D2AD, C324, F1BF, E036,18C1, 0948, 3BD3, 2A5A, 5EE5, 4F6C, 7DF7, 6C7E," & _
"A50A, B483, 8618, 9791, E32E, F2A7, C03C, D1B5,2942, 38CB, 0A50, 1BD9, 6F66, 7EEF, 4C74, 5DFD," & _
"B58B, A402, 9699, 8710, F3AF, E226, D0BD, C134,39C3, 284A, 1AD1, 0B58, 7FE7, 6E6E, 5CF5, 4D7C," & _
"C60C, D785, E51E, F497, 8028, 91A1, A33A, B2B3,4A44, 5BCD, 6956, 78DF, 0C60, 1DE9, 2F72, 3EFB," & _
"D68D, C704, F59F, E416, 90A9, 8120, B3BB, A232,5AC5, 4B4C, 79D7, 685E, 1CE1, 0D68, 3FF3, 2E7A," & _
"E70E, F687, C41C, D595, A12A, B0A3, 8238, 93B1,6B46, 7ACF, 4854, 59DD, 2D62, 3CEB, 0E70, 1FF9," & _
"F78F, E606, D49D, C514, B1AB, A022, 92B9, 8330,7BC7, 6A4E, 58D5, 495C, 3DE3, 2C6A, 1EF1, 0F78 "
Items = Split(s, ",")
n = UBound(Items)
ReDim CrcTab16(n)
For i = 0 To n
CrcTab16(i) = Val("&h" & Items(i) & "&")
Next
End Sub
模块代码:
Option Explicit
Public CrcTab16() As Long
Public Function GetCrc16(pData() As Byte, nLength As Long) As Long
'U16 GetCrc16(const U8* pData, int nLength)
'{
' U16 fcs = 0xffff; // 初始化
' while(nLength>0){
' fcs = (fcs >> 8) ^ crctab16[(fcs ^ *pData) & 0xff];
' nLength--;
' pData++;
' }
' return ~fcs; // 取反
'}
Dim fcs As Long
Dim Idx As Long
fcs = &HFFFF&
While nLength > 0
fcs = (fcs \ 256) Xor CrcTab16((fcs Xor pData(Idx)) And &HFF&)
Idx = Idx + 1
nLength = nLength - 1
Wend
GetCrc16 = (Not fcs) And &HFFFF&
End Function
补充:VB , 基础类