Jump to content
Fórum Script Brasil
  • 0

Calculo do CRC


Idelbrandes Gonçalves de Amorim

Question

Bom dia pessoal, convertir essa função do C# para vba ele funciona perfeitamento no excel e access
porem estou tentendo usar ela no vb.net  editor VISUAL STUDIO 2013, aparece o erro conforme a imagem enexo, alguém consegue corrigir esse erro;
Function crc_ccitt_ffff(strParam As String) As String
        Const CRC_POLY_CCITT As Long = &H1021&
        Const CRC_START_CCITT_FFFF As Long = &HFFFF&
        Dim crc As Long, b() As Byte, c As Long, i As Long, j As Long
        Dim crc_tabccitt(0 To 255) As Long
        For i = 0 To 255
            crc = 0
            c = i * 256
            For j = 0 To 7
                If (crc Xor c) And 32768 Then
                    crc = (crc * 2) Xor CRC_POLY_CCITT
                Else
                    crc = crc * 2
                End If
                c = c * 2
            Next j
            crc_tabccitt(i) = crc
        Next i
            b = strParam
        crc = CRC_START_CCITT_FFFF
        For i = 0 To UBound(b) Step 2
            crc = (crc * 256) Xor crc_tabccitt(((crc \ 256) Xor b(i)) And 255)
            crc = ((crc \ 65536) * 65536) Xor crc
        Next i
        crc_ccitt_ffff = Hex(crc)
    End Function

crc_ccitt7P00BCTQ6X.jpg

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

The code you provided seems to be a CRC-CCITT (Cyclic Redundancy Check) implementation in VBA (Visual Basic for Applications). However, there might be a small issue in the conversion, specifically with the way VBA handles byte arrays and certain bitwise operations. Here's a corrected version of the code:

 
 
Function crc_ccitt_ffff(strParam As String) As String
    Const CRC_POLY_CCITT As Long = &H1021&
    Const CRC_START_CCITT_FFFF As Long = &HFFFF&
    Dim crc As Long, c As Long, i As Long, j As Long
    Dim crc_tabccitt(0 To 255) As Long
    Dim b() As Byte
    
    For i = 0 To 255
        crc = 0
        c = i * 256
        For j = 0 To 7
            If (crc Xor c) And 32768 Then
                crc = (crc * 2) Xor CRC_POLY_CCITT
            Else
                crc = crc * 2
            End If
            c = c * 2
        Next j
        crc_tabccitt(i) = crc
    Next i
    
    b = StrConv(strParam, vbFromUnicode) ' Convert the string to a byte array
    
    crc = CRC_START_CCITT_FFFF
    For i = LBound(b) To UBound(b) Step 2
        crc = (crc * 256) Xor crc_tabccitt(((crc \ 256) Xor b(i)) And 255)
        crc = ((crc \ 65536) * 65536) Xor crc
    Next i
    
    crc_ccitt_ffff = Hex(crc)
End Function
  1. Used
    StrConv
    to convert the input string to a byte array (vbFromUnicode).
  2. Adjusted the loop to iterate over the entire byte array.

Please try using this corrected version in your VB.NET editor in Visual Studio 2013 and see if it resolves the issue you were facing.adobe reader download gratis

Edited by ativadore
just add coding
Link to comment
Share on other sites

  • 0

O erro que você está enfrentando no Visual Studio 2013 ao tentar usar essa função em VB.NET provavelmente está relacionado à conversão de tipos e manipulação de bytes. Aqui está a função convertida para VB.NET:

 

Function crc_ccitt_ffff(strParam As String) As String
    Const CRC_POLY_CCITT As Integer = &H1021
    Const CRC_START_CCITT_FFFF As Integer = &HFFFF
    Dim crc As Integer = CRC_START_CCITT_FFFF
    Dim crc_tabccitt(255) As Integer
    Dim i As Integer, j As Integer

    For i = 0 To 255
        crc = 0
        Dim c As Integer = i * 256
        For j = 0 To 7
            If ((crc Xor c) And 32768) Then
                crc = (crc * 2) Xor CRC_POLY_CCITT
            Else
                crc = crc * 2
            End If
            c = c * 2
        Next
        crc_tabccitt(i) = crc
    Next

    Dim bytes As Byte() = System.Text.Encoding.ASCII.GetBytes(strParam)
    For i = 0 To bytes.Length - 1 Step 2
        crc = (crc * 256) Xor crc_tabccitt(((crc \ 256) Xor bytes(i)) And 255)
        crc = ((crc \ 65536) * 65536) Xor crc
    Next

    Return crc.ToString("X")
End Function

Agora, essa função de código escrita pelo meu colega com penalty shoot out está em VB.NET e deve funcionar corretamente no Visual Studio 2013. Certifique-se de usar o mesmo tipo de projeto (Windows Forms, aplicativo de console, etc.) para evitar problemas de contexto de código.

Edited by BlackLion33
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152.1k
    • Total Posts
      651.9k
×
×
  • Create New...