基于某些C ++ CRC8代码我尝试在VBA中编写它,也借助我通过Google搜索找到的一些示例。
我使用此代码:
Private Function CalculateCRC8(crc8 As Integer, Character As String) As Byte
Dim l_BitPopped As Boolean
Dim l_Bit As Byte
Dim l_Char As Integer
l_Char = Asc(Character)
For l_Bit = 0 To 7
l_BitPopped = (crc8 And &H80)
crc8 = crc8 * 2
If l_Char And &H80 Then
crc8 = crc8 Or &H1
Else
crc8 = crc8 And &HFE
End If
If l_BitPopped Then
crc8 = crc8 Xor &H85
End If
l_Char = l_Char * 2
Next
CalculateCRC8 = crc8 And &HFF
End Function
Private Function GetCRC8(crc8 As String) As Integer
Dim l_Len As Integer
Dim l_Char As String
Dim l_CRC8 As Integer
MsgBox "CRC8:" & crc8
l_CRC8 = 0
For l_Len = 1 To Len(crc8)
l_Char = Mid(crc8, l_Len, 1)
l_CRC8 = CalculateCRC8(l_CRC8, l_Char)
Next
GetCRC8 = l_CRC8
End Function
我输入一个字符串:
A00000039656434103F0154020D4000A
我得到CRC8 = 10
的结果
然而,在我收到的例子中,它应该是CRC8 = 6D
在我的例子中,没有代码示例如何计算CRC8,我刚刚收到了一个如何工作的例子。 " A"是十六进制值,而不是char。
我是否需要将整个 STRING 转换为 BINARY 以便能够执行CRC8?
希望somene可以提供帮助。