我正在用 Excel VBA 进行 CRC8 计算,这让我很头疼。我在 VBA 中编写了一个函数,它返回 CRC8 值,稍后可以将其存储到单元格中。但是,在打印相同内容时,我收到错误消息“OverFlow”。
我在“ShiftLeft = Num * (2 ^ Places)”处的函数中出现溢出
Function CRCPrateek(CRCrng As Range) As Integer
Dim CRC As Integer
Dim length As Integer
Dim Hexbyte As Integer
Dim i As Integer
'Initial CRC seed is Zero CRC = H00
'The real part of the CRC. Where I commented "Polynomial", it used to be a # define
'Polynomial 7. 'I replaced the word Polynomial with 7, however that means the 7 may
'be subject to change depending on the version of the crc you are running.
'To loop it for each cell in the range
For Each cel In CRCrng
'Verify if there is atleast one cell to work on
If Len(cel) > 0 Then
Hexbyte = cel.Value
CRC = CRC Xor Hexbyte
For i = 0 To 7
If Not CRC And H80 Then
CRC = ShiftLeft(CRC, 1)
CRC = CRC Xor 7
Else
CRC = ShiftLeft(CRC, 1)
End If
Next
End If
Next
CRCPrateek = CRC
End Function
Function ShiftLeft(Num As Integer, Places As Integer) As Integer
ShiftLeft = Num * (2 ^ Places)
End Function
您正在处理 2 字节整数,而不是字节。此外,我怀疑 cell.value 确实是一个十六进制值 - 而不是像“3F”这样的十六进制字符串,您必须首先将其转换为数值(在 0..255 范围内)。
要创建真正的字节数组和字节 CRC,请使用此解决方案:在 VBA 中计算 CRC8
在那里你会找到溢出的解决方案,即在移位之前屏蔽最高位。
别人的努力和建议帮助我找到了正确的答案;我发布了我编写的用于计算 CRC8 的通用函数。它给了我想要的结果,并且我还对照其他 CRC 计算器对其进行了检查。
'GENERATE THE CRC
Function CRCPrateek(ByVal crcrng As Range) As Long
Dim crc As Byte
Dim length As Byte
Dim Hexbyte As String
Dim DecByte As Byte
Dim i As Byte
' Initial CRC seed is Zero
crc = &H0
'The real part of the CRC. Where I commented "Polynomial", it used to be a # define
'Polynomial 7. I replaced the word Polynomial with 7, however that means the 7 may
'be subject to change depending on the version of the crc you are running.
'To loop it for each cell in the range
For Each cel In crcrng
'Verify if there is atleast one cell to work on
' If Len(cel) > 0 Then
DecByte = cel.Value
crc = crc Xor DecByte
For i = 0 To 7
If ((crc And &H80) <> 0) Then
crc = ShiftLeft(crc, 1)
crc = crc Xor 7
Else
crc = ShiftLeft(crc, 1)
End If
Next
' End If
Next
CRCPrateek = crc
End Function
Function ShiftLeft(ByVal Num As Byte, ByVal Places As Byte) As Byte
ShiftLeft = ((Num * (2 ^ Places)) And &HFF)
End Function
'END OF CRC
在调用上述函数时,您需要在此处作为参数传递的唯一内容是单元格的范围(具有小数(在单元格中使用 HEX2DEC)值。
'EXAMPLE CALL TO CRC FUNCTION FROM A SUB
'select the crc Range
Set crcrng = Range("I88", "U88")
crc = CRCPrateek(crcrng)
Sheet1.Cells(88, 22).Value = crc
MsgBox ("CRC value is " & Sheet1.Cells(86, 22).Value & "(in HEX) ")
注意:此函数将输入值视为小数,以十进制计算 CRC 值,稍后返回 CRC 值后,您可以将其存储在任何其他单元格中,并使用单元格中的公式 DEC2HEX 转换回十六进制
请注意,无需使用 VBA,即可在 Excel 中使用新的溢出公式(LAMBDA、LET 等,请参阅this 和 that)计算 CRC,受 Excel 的字符串长度限制 的限制,无法计算长度有限的常规 ASCII 字符串CRC16 为 4093。
复制此公式,根据需要仅修改前四行。
REDUCE
更改为SCAN
即可查看多项式除法的计算步骤。formatHex
更改为 formatByte
以显示二进制。0x8005
,它转换为10000000_00000101
,但实际的多项式除数是1_10000000_00000101
。我的下面的公式需要完整的除数,以便在格式化校验和时可以自动包含正确数量的前导零。我还没有实现Init
XorOut
;该公式假设两者都是 0
。扩展它应该不难,但我没有。
=LET(
messageAscii, $A30,
polyBinary, "11000000000000101",
reflectInput, TRUE,
reflectResult, TRUE,
ascii2bin,LAMBDA(a,CONCAT(BASE(CODE(MID(a, SEQUENCE(LEN(a)), 1)),2,8))),
binTrimLeadingZeros, LAMBDA(a,TRIM(MID(a,FIND("1",a),LEN(a)))),
delimitedRows, LAMBDA(rowInterval,paddingInterval,paddingChar,rowLambda,string,
LET(stringCharArray, MID(string,SEQUENCE(LEN(string)),1),
qtyOfPadding, MOD(LEN(string),paddingInterval),
paddingCharArray, IF(SEQUENCE(paddingInterval - qtyOfPadding), paddingChar),
paddedStringCharArray, IF(qtyOfPadding=0, stringCharArray, VSTACK(paddingCharArray, stringCharArray)),
BYROW(WRAPROWS(paddedStringCharArray, rowInterval, ""), rowLambda))),
addDelimiterEveryXChar, LAMBDA(delimiterInterval,delimiterChar,paddingChar,string,
TEXTJOIN(delimiterChar, TRUE, delimitedRows(delimiterInterval,delimiterInterval,paddingChar,LAMBDA(row, CONCAT(row)),string))),
reflectString, LAMBDA(a,CONCAT(MID(a,SEQUENCE(LEN(a),,LEN(a),-1),1))),
reflectStringByGroup, LAMBDA(delimiterInterval,paddingChar,string,
TEXTJOIN("", TRUE, delimitedRows(delimiterInterval,delimiterInterval,paddingChar,LAMBDA(row, reflectString(CONCAT(row))),string))),
swapEndiannessSingle, LAMBDA(string,
LET(theseDelimitedRows,delimitedRows(8,16,"0",LAMBDA(row, CONCAT(row)),string),
TEXTJOIN("", TRUE, CHOOSEROWS(theseDelimitedRows, TOCOL(CHOOSECOLS(SEQUENCE(COUNTA(theseDelimitedRows)/2,2),2,1)))))),
swapEndianness, LAMBDA(a, MAP(a, LAMBDA(cell, swapEndiannessSingle(cell)))),
nbit2dec, LAMBDA(a,SUM(MID( a, SEQUENCE(LEN(a)),1)*2^SEQUENCE(LEN(a),1,LEN(a)-1,-1))),
polyPrepped, binTrimLeadingZeros(polyBinary),
formatByte, LAMBDA(a, MAP(a, LAMBDA(cell, addDelimiterEveryXChar(8, "_", "0", cell)))),
formatHex, LAMBDA(a, MAP(a, LAMBDA(cell, DEC2HEX(nbit2dec(cell),ROUNDUP((LEN(polyPrepped)-1)/4,0))))),
crc,LAMBDA(messageBin,polyBin,reflectInput,reflectResult,
LET(reflectCrcInput, LAMBDA(a, MAP(a, LAMBDA(cell, reflectStringByGroup(8,"0",cell)))),
lenPoly, LEN(polyPrepped),
reflectCrcResult, LAMBDA(a, MAP(a, LAMBDA(cell, reflectStringByGroup(lenPoly-1,"0",cell)))),
messagePrepped, (CONCAT(IF(reflectInput,reflectCrcInput(messageBin),messageBin), SEQUENCE(lenPoly-1,,0,0))),
polynomialDivision, REDUCE(LEFT(messagePrepped,lenPoly),SEQUENCE(LEN(messagePrepped)-lenPoly+1,,0),
LAMBDA(a,i,CONCAT(IF((nbit2dec(a)>=POWER(2,lenPoly-1)),
BASE(BITXOR(nbit2dec(a), nbit2dec(polyPrepped)),2), RIGHT(a,lenPoly-1)),
MID(messagePrepped,lenPoly+1+i,1)))),
IF(reflectResult,reflectCrcResult(polynomialDivision),polynomialDivision))),
formatHex(crc(ascii2bin(messageAscii),binTrimLeadingZeros(polyBinary),reflectInput,reflectResult))
)