Calculating CRC8 in VBA

Unfortunately, I have to admit that I gave up on 8 hours of searching for the correct CRC8 code in the VBA programming language. There are many examples, but I haven't found one that works in my case. So here I am, asking for your help if someone can write me this piece of code or if there is a mystical link that I have not clicked on.

Explanation:

AppID = "A00000039656434103F0154020D4000A" 

      

My project requires char "A" to be at the end of this AppID, since based on this CRC8 should be calculated. If you understand correctly (because I might go crazy all day trying to write this CRC8 function), I have an ID 32-byte

on which I want to perform a CRC8 check on 16bits

(does this make sense?)

In this example, I only have the CRC8 return result:

CRC8 = 0x6D

      

And I need to replace the bottom nible with char "A" in my main AppID:

FinalAppID = "A00000039656434103F0154020D4000D"

      

PROBLEM: But I just don't know how to write and convert codes from C ++ / C #. And I did set up in stages, but it didn't work.

This is the code I am using:

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

      

Now I am not in the office, so there may be typos in the above code or some silly mistakes, I wrote this now that I have been working with it all day.

Problem with the previous code:

Mistake:

Error: Overflow!

      

This error occurs even if I pass entire string

or just 16bits

. The same error.

If anyone can help me, I will be very grateful to him!

+3


source to share


1 answer


Here is a multi-fix version and overflow prevention. It generates the expected output (& H6D) for your six bytes (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

      



This function takes a hex string and interprets it as an array 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

      

+4


source







All Articles