首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Excel的CRC8计算

Excel的CRC8计算
EN

Stack Overflow用户
提问于 2015-10-17 11:20:20
回答 2查看 8.5K关注 0票数 0

我正在用Excel进行CRC8计算。我用VBA编写了一个函数,它返回CRC8值,可以在以后存储到单元格中。但是,在打印相同的内容时,我在说"OverFlow“时出错了。

在"ShiftLeft = Num * (2 ^ Places)“的函数中溢出

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-10-26 05:42:39

其他人的努力和建议帮助我找到了正确的答案;我正在发布我为计算CRC8而编写的通用函数。它给了我想要的结果&我也和其他CRC计算器核对过。

代码语言:javascript
复制
'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 )值。

代码语言:javascript
复制
'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将其转换为十六进制。

票数 0
EN

Stack Overflow用户

发布于 2015-10-17 18:21:58

你在做的是2字节整数,而不是字节。此外,我怀疑cell.value确实是一个十六进制值--而是像'3F‘这样的十六进制字符串,您必须首先将其转换为一个数值(在0..255范围内)。

若要创建真正的字节数组和字节CRC,则授予此解决方案:Calculating CRC8 in VBA

在那里,您将找到溢出的解决方案,即在移位前隐藏最高位。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33186021

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档