在VBA计算CRC8 [英] Calculating CRC8 in VBA
问题描述
所以,可悲的是我不得不承认,我放弃了8小时搜正确的CRC8 code在VBA编程语言。 有很多例子,但我却没有找到一个,在我的情况下工作。所以我在这里,问你的帮助,如果有人可以给我写这部分的code,或者如果有一个misterious链接,我没有点击。
So sadly I have to admit that I gave up on 8 hour search for the correct CRC8 code in VBA programming language. There are many examples, but yet I haven't find the one that works in my case. So here I am, asking you for help, if someone can write me this part of code, or if there's a misterious link, that I haven't clicked on.
说明:
AppID = "A00000039656434103F0154020D4000A"
在我的项目,则要求炭A是在此的AppID结束时,因为在此基础上的CRC8应计算。如果理解正确的我有 32字节
ID,(因为我可能已经在试图写这个CRC8函数整天疯了),我想做的CRC8检查上在 16位
(这是否有意义?)
In my project, it is required that char "A" is at the end of this AppID, since based on this the CRC8 should be calculated. If understand correct (cause I might have gone insane during entire day of trying to write this CRC8 function) I have 32-byte
ID, on which I want to do the CRC8 check on the 16bits
(does this make sense?)
在给定的例子,我有一个什么样的CRC8应该只返回结果:
In the given example, I have only the result of what the CRC8 should return:
CRC8 = 0x6D
和我需要在我的主要的AppID更换下nible与字符A:
And I need to replace the lower nible with the char "A" in my main AppID:
FinalAppID = "A00000039656434103F0154020D4000D"
问题:但是,我根本不知道怎么写,也没有从C ++ / C#转换为codeS。我真的一步一步转换陈吉伟,但没有奏效。
PROBLEM: But I simply don't know how to write nor to convert the codes from C++ / C#. And I was really strick with step by step conversion, yet it didn't work.
这是code我用:
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
我不在办公室了,所以有可能会被拼写错误,在上述code,或者一些愚蠢的错误,我写它只是现在我的头,因为我曾与它整整一天。
这是发生上述code的问题是:
The problem that occurs with above code is:
错误:
Error: Overflow!
即使我通过了出现此错误的整个字符串
,或只是 16位
。同样的错误。
This error occurs even if I pass the entire string
, or just the 16bits
. Same error.
如果任何人有任何事情来帮助我在这里,我将非常感激他!
If anyone has anything to help me out here, I will be really grateful to him!
推荐答案
下面是一个版本,有一些修正,并$ P $的发生pvents溢出。它产生预期的结果(安培; H6D)。为您的十六进制字节(A00000039656434103F0154020D4000A)
Here is a version of with a few fixes and it prevents the overflow from happening. It generates the expected result (&H6D) for your hex 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
此功能需要一个十六进制字符串,跨$ P $点作为一个字节
阵列。
This function takes a hexadecimal string and interprets it as a Byte
array.
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
这篇关于在VBA计算CRC8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!