Ir para conteúdo
Fórum Script Brasil
  • 0

Calculo do CRC


Idelbrandes Gonçalves de Amorim

Pergunta

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 para o comentário
Compartilhar em outros sites

2 respostass a esta questão

Posts Recomendados

  • 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

Editado por ativadore
just add coding
Link para o comentário
Compartilhar em outros 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.

Editado por BlackLion33
Link para o comentário
Compartilhar em outros sites

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152,1k
    • Posts
      651,8k
×
×
  • Criar Novo...