在VBA中计算CRC8

时间:2014-10-30 22:09:36

标签: vba ms-access access-vba

很遗憾,我不得不承认,我放弃了8小时搜索VBA编程语言中正确的CRC8代码。 有很多例子,但我找不到适用于我的案例。所以我在这里,请求您的帮助,如果有人可以给我写这部分代码,或者如果有一个神秘的链接,我没有点击。

解释

AppID = "A00000039656434103F0154020D4000A" 

在我的项目中,要求char“A”位于此AppID的末尾,因为基于此,应计算CRC8。如果理解正确(因为我可能在尝试编写此CRC8函数的整天中感到疯狂)我有32-byte ID,我想在16bits上进行CRC8检查(这样做感?)

在给定的例子中,我只得到了CRC8应该返回的结果:

CRC8 = 0x6D

我需要用我的主AppID中的char“A”替换下层nible:

FinalAppID = "A00000039656434103F0154020D4000D"

问题:但我根本不知道如何编写或转换C ++ / C#中的代码。我一步步转换真的很苛刻,但它没有用。

这是我使用的代码:

Public function calculateCRC8(ByVal AppID As String ) As String
    Dim CRC8 As Byte
    Dim i as Integer
    Dim j as Integer
    Dim AppIDarray()


    CRC8 = &HC7; //Based on preset 0xE3 
    aidLenght = LEN(AppID)
    AppIDarray = StringToArray(AppID) ' I user a UDF that I wrote, this should work OK'
            For j = 0 To aidLenght
                CRC8 = CRC8 Xor AppIDarray(j) 
                For i = 1 To 8 
                    If CRC8 And &H80 Then
                     CRC8 = (CRC8 * 2) Xor &H1D
                    Else
                     CRC8 = CRC8 * 2
                    End If  
                next i
            Next j
    calculateCRC8 = CRC8
End Function

我现在不在办公室,因此上面的代码中可能存在拼写错误,或者是一些愚蠢的错误,我刚才在脑海里写这篇文章,就像我整天都在使用它一样。

上述代码出现的问题是:

错误:

Error: Overflow!

即使我通过entire string16bits,也会发生此错误。同样的错误。

如果有人有任何帮助我的话,我将非常感激他!

1 个答案:

答案 0 :(得分:4)

这是一个包含一些修复的版本,它可以防止溢出发生。它为您的十六进制字节生成预期结果(& H6D)(A00000039656434103F0154020D4000A)。

Public Function calculateCRC8(ByVal AppID As String) As String
    Dim CRC8 As Byte
    Dim i As Integer
    Dim j As Integer
    Dim AppIDarray() As Byte  '<--- explicitly dimensioned as a Byte array to avoid confusion


    CRC8 = &HC7  

    'The AppID is actually bytes stored in hexadecimal in a string. You have to convert them back to bytes before you can run a crc8 on them.
    AppIDarray = HexToByte(AppID)
    aidLength = UBound(AppIDarray)
            For j = 0 To aidLength
                CRC8 = CRC8 Xor AppIDarray(j)
                For i = 1 To 8
                    If CRC8 And &H80 Then
                     'masking off the left-most bit before shifting prevents the Overflow error.
                     CRC8 = ((&H7F And CRC8) * 2) Xor &H1D
                    Else
                     CRC8 = CRC8 * 2
                    End If
                Next i
            Next j
    calculateCRC8 = CRC8
End Function

此函数采用十六进制字符串并将其解释为Byte数组。

Public Function HexToByte(strHex As String) As Byte()
    Dim i As Integer
    Dim tempByte As Byte
    Dim outBytes() As Byte
    ReDim outBytes(Len(strHex) \ 2 - 1)
    For i = 0 To Len(strHex) \ 2 - 1
        For j = 0 To 1
            char = Mid(strHex, i * 2 + j + 1, 1)
            Select Case char
                Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9":
                    tempByte = tempByte Or (Asc(char) - 48)
                Case "A", "B", "C", "D", "E", "F":
                    tempByte = tempByte Or (Asc(char) - 55)
            End Select
            If j = 0 Then
                tempByte = tempByte * 2 ^ 4
            Else
                outBytes(i) = tempByte
                tempByte = 0
            End If
        Next
    Next
    HexToByte = outBytes
End Function