Foxtable(狐表)用户栏目专家坐堂 → [求助]求CRC16 校验码算法例子


  共有9322人关注过本帖树形打印复制链接

主题:[求助]求CRC16 校验码算法例子

帅哥哟,离线,有人找我吗?
有点甜
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2014/12/22 19:04:00 [显示全部帖子]

Dim s As String = CRCCheckFile("d:\test.jpg")
Output.Show(s)

 

http://www.foxtable.com/help/topics/1346.htm

 


 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2014/12/22 19:16:00 [显示全部帖子]

 全局代码

 

Public CRC16Table As UShort() = {&H0, &H1021, &H2042, &H3063, &H4084, &H50a5, _
 &H60c6, &H70e7, &H8108, &H9129, &Ha14a, &Hb16b, _
 &Hc18c, &Hd1ad, &He1ce, &Hf1ef, &H1231, &H210, _
 &H3273, &H2252, &H52b5, &H4294, &H72f7, &H62d6, _
 &H9339, &H8318, &Hb37b, &Ha35a, &Hd3bd, &Hc39c, _
 &Hf3ff, &He3de, &H2462, &H3443, &H420, &H1401, _
 &H64e6, &H74c7, &H44a4, &H5485, &Ha56a, &Hb54b, _
 &H8528, &H9509, &He5ee, &Hf5cf, &Hc5ac, &Hd58d, _
 &H3653, &H2672, &H1611, &H630, &H76d7, &H66f6, _
 &H5695, &H46b4, &Hb75b, &Ha77a, &H9719, &H8738, _
 &Hf7df, &He7fe, &Hd79d, &Hc7bc, &H48c4, &H58e5, _
 &H6886, &H78a7, &H840, &H1861, &H2802, &H3823, _
 &Hc9cc, &Hd9ed, &He98e, &Hf9af, &H8948, &H9969, _
 &Ha90a, &Hb92b, &H5af5, &H4ad4, &H7ab7, &H6a96, _
 &H1a71, &Ha50, &H3a33, &H2a12, &Hdbfd, &Hcbdc, _
 &Hfbbf, &Heb9e, &H9b79, &H8b58, &Hbb3b, &Hab1a, _
 &H6ca6, &H7c87, &H4ce4, &H5cc5, &H2c22, &H3c03, _
 &Hc60, &H1c41, &Hedae, &Hfd8f, &Hcdec, &Hddcd, _
 &Had2a, &Hbd0b, &H8d68, &H9d49, &H7e97, &H6eb6, _
 &H5ed5, &H4ef4, &H3e13, &H2e32, &H1e51, &He70, _
 &Hff9f, &Hefbe, &Hdfdd, &Hcffc, &Hbf1b, &Haf3a, _
 &H9f59, &H8f78, &H9188, &H81a9, &Hb1ca, &Ha1eb, _
 &Hd10c, &Hc12d, &Hf14e, &He16f, &H1080, &Ha1, _
 &H30c2, &H20e3, &H5004, &H4025, &H7046, &H6067, _
 &H83b9, &H9398, &Ha3fb, &Hb3da, &Hc33d, &Hd31c, _
 &He37f, &Hf35e, &H2b1, &H1290, &H22f3, &H32d2, _
 &H4235, &H5214, &H6277, &H7256, &Hb5ea, &Ha5cb, _
 &H95a8, &H8589, &Hf56e, &He54f, &Hd52c, &Hc50d, _
 &H34e2, &H24c3, &H14a0, &H481, &H7466, &H6447, _
 &H5424, &H4405, &Ha7db, &Hb7fa, &H8799, &H97b8, _
 &He75f, &Hf77e, &Hc71d, &Hd73c, &H26d3, &H36f2, _
 &H691, &H16b0, &H6657, &H7676, &H4615, &H5634, _
 &Hd94c, &Hc96d, &Hf90e, &He92f, &H99c8, &H89e9, _
 &Hb98a, &Ha9ab, &H5844, &H4865, &H7806, &H6827, _
 &H18c0, &H8e1, &H3882, &H28a3, &Hcb7d, &Hdb5c, _
 &Heb3f, &Hfb1e, &H8bf9, &H9bd8, &Habbb, &Hbb9a, _
 &H4a75, &H5a54, &H6a37, &H7a16, &Haf1, &H1ad0, _
 &H2ab3, &H3a92, &Hfd2e, &Hed0f, &Hdd6c, &Hcd4d, _
 &Hbdaa, &Had8b, &H9de8, &H8dc9, &H7c26, &H6c07, _
 &H5c64, &H4c45, &H3ca2, &H2c83, &H1ce0, &Hcc1, _
 &Hef1f, &Hff3e, &Hcf5d, &Hdf7c, &Haf9b, &Hbfba, _
 &H8fd9, &H9ff8, &H6e17, &H7e36, &H4e55, &H5e74, _
 &H2e93, &H3eb2, &Hed1, &H1ef0}

Public Function CRC16(dat As [Byte](), count As Integer) As Integer
 Dim crc As Integer = 0
 Dim crctemp As [Byte]
 Dim i As Integer = 0
 While count > 0
  count -= 1
  crctemp = Convert.ToByte(crc >> 8)
  crc = (crc << 8) And &Hffff
  crc = (CRC16Table(crctemp Xor dat(i)) Xor crc) And &Hffff
  i += 1
 End While
 Return crc
End Function

 

 

调用代码

 

Dim aaff As [Byte]() = {&H01, &H04, &H00, &H00, &H00, &H0a}
Dim aa As Integer = CRC16(aaff, aaff.Length)
Dim bb As String = Convert.ToString(aa, 16).ToUpper()
msgbox(bb)


 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2014/12/23 11:13:00 [显示全部帖子]

 无法帮你测试,你的结果是错的。请贴出运算规则,你的肯定不是CRC16的标准计算。

 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  4楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2014/12/23 18:03:00 [显示全部帖子]

 全局代码

 

Public chCRCHTalbe As UInteger() = {&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, &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, &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}

Public chCRCLTalbe As Uinteger() = {&H0, &Hc0, &Hc1, &H1, &Hc3, &H3, _
&H2, &Hc2, &Hc6, &H6, &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, &Hd9, _
&H1b, &Hdb, &Hda, &H1a, &H1e, &Hde, _
&Hdf, &H1f, &Hdd, &H1d, &H1c, &Hdc, _
&H14, &Hd4, &Hd5, &H15, &Hd7, &H17, _
&H16, &Hd6, &Hd2, &H12, &H13, &Hd3, _
&H11, &Hd1, &Hd0, &H10, &Hf0, &H30, _
&H31, &Hf1, &H33, &Hf3, &Hf2, &H32, _
&H36, &Hf6, &Hf7, &H37, &Hf5, &H35, _
&H34, &Hf4, &H3c, &Hfc, &Hfd, &H3d, _
&Hff, &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, &H64, &Ha4, _
&H6c, &Hac, &Had, &H6d, &Haf, &H6f, _
&H6e, &Hae, &Haa, &H6a, &H6b, &Hab, _
&H69, &Ha9, &Ha8, &H68, &H78, &Hb8, _
&Hb9, &H79, &Hbb, &H7b, &H7a, &Hba, _
&Hbe, &H7e, &H7f, &Hbf, &H7d, &Hbd, _
&Hbc, &H7c, &Hb4, &H74, &H75, &Hb5, _
&H77, &Hb7, &Hb6, &H76, &H72, &Hb2, _
&Hb3, &H73, &Hb1, &H71, &H70, &Hb0, _
&H50, &H90, &H91, &H51, &H93, &H53, _
&H52, &H92, &H96, &H56, &H57, &H97, _
&H55, &H95, &H94, &H54, &H9c, &H5c, _
&H5d, &H9d, &H5f, &H9f, &H9e, &H5e, _
&H5a, &H9a, &H9b, &H5b, &H99, &H59, _
&H58, &H98, &H88, &H48, &H49, &H89, _
&H4b, &H8b, &H8a, &H4a, &H4e, &H8e, _
&H8f, &H4f, &H8d, &H4d, &H4c, &H8c, _
&H44, &H84, &H85, &H45, &H87, &H47, _
&H46, &H86, &H82, &H42, &H43, &H83, _
&H41, &H81, &H80, &H40}

Public Function CRC16(pchMsg As uinteger(), wDataLen As ushort) As ushort
Dim chCRCHi As uinteger = &Hff
' 高CRC 字节初始化
Dim chCRCLo As uinteger = &Hff
' 低CRC 字节初始化
Dim wIndex As ushort
Dim i As ushort = 0
' CRC 循环中的索引
While wDataLen > 0
    ' 计算CRC
    wIndex = chCRCLo Xor pchMsg(i)
    i += 1
    chCRCLo = chCRCHi Xor chCRCHTalbe(wIndex)
    chCRCHi = chCRCLTalbe(wIndex)
    wDataLen -= 1
End While
Return ((chCRCHi << 8) Or chCRCLo)

End Function

[此贴子已经被作者于2018/11/16 10:43:10编辑过]

 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  5楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2014/12/23 18:04:00 [显示全部帖子]

调用

 

Dim aaff As uInteger() = {&H01, &H03, &H04, &H01, &Hf4, &H13, &H88}
Dim aa As Integer = CRC16(aaff, aaff.Length)
Dim bb As String = Convert.ToString(aa, 16).ToUpper()
msgbox(bb)

[此贴子已经被作者于2018/11/16 10:46:13编辑过]

 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  6楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2014/12/24 10:56:00 [显示全部帖子]

 你给的算法有问题。

 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  7楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2014/12/24 19:35:00 [显示全部帖子]

全局代码

 

Public Function calccrc(crcbuf As Uinteger, crc As Uinteger) As Uinteger
Dim i As Uinteger
crc = crc Xor crcbuf
For i = 0 To 7
 Dim chk As Uinteger
 chk = crc And 1
 crc = crc >> 1
 crc = crc And &H7fff
 If chk = 1 Then
  crc = crc Xor &Ha001
 End If
 crc = crc And &Hffff
Next
Return crc
End Function

Public Function chkcrc(buf As Uinteger(),len As Uinteger) As Uinteger
Dim hi As Uinteger, lo As Uinteger
Dim i As Uinteger
Dim crc As Uinteger
crc = &Hffff
For i = 0 To len - 1
 crc = calccrc(buf(i), crc)
Next
hi = crc Mod 256
lo = crc / 256
crc = (hi << 8) Or lo
Return crc
End Function

 

 

调用代码

 

Dim aaff As uInteger() = {&H01, &H04, &H00, &H00, &H00, &H0a}
Dim aa As Integer = chkcrc(aaff, aaff.Length)
Dim bb As String = Convert.ToString(aa, 16).ToUpper()
msgbox(bb)


 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  8楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2015/3/22 12:07:00 [显示全部帖子]

算法有问题,换一种算法

 

Public Function crc_16(Array As UInteger(), Len As UInteger) As UInteger
 Dim IX As UInteger, IY As UInteger, CRC As UInteger
        Dim Rcvbuf(1) As UInteger
 CRC = &Hffff
 'set all 1
 If Len <= 0 Then
  CRC = 0
 Else
  Len -= 1
  

  For IX = 0 To Len
   CRC = CRC Xor (Array(IX))
   For IY = 0 To 7
    If (CRC And 1) <> 0 Then
     CRC = (CRC >> 1) Xor &Ha001
    Else
     CRC = CRC >> 1
     '
    End If
   Next

  Next
 End If
 Rcvbuf(0) = (CRC And &Hff00) >> 8
 '高位置
 Rcvbuf(1) = (CRC And &Hff)
 '低位置
 CRC = Rcvbuf(0) << 8
 CRC += Rcvbuf(1)
 Return CRC
End Function

 

 

----------------

 

Dim aaff As uInteger() = {&H55, &H05, &H01}
Dim aa As Integer = crc_16(aaff, aaff.Length)
Dim bb As String = Convert.ToString(aa, 16).ToUpper()
msgbox(bb)


 回到顶部