【发布时间】:2015-10-17 11:20:20
【问题描述】:
我正在使用 Excel VBA 进行 CRC8 计算。我在 VBA 中编写了一个函数,它返回 CRC8 值,以后可以将其存储到一个单元格中。但是,在打印相同的内容时,我收到一条错误消息,提示“溢出”。
我在“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
【问题讨论】:
-
将所有 Integer 声明更改为 Long。
-
但是我得到一个 1 字节的 CRC 吗?根据我的理解,我应该为 CRC8 _ATM 获得 1 个字节。
-
我将所有声明更改为 byte & long 但仍然出现溢出错误。