CRC16_CCITT PIX EM VB6 OU VB.NET

SINCLAIR 27/07/2023 17:05:01
#501615
Prezados,

Tenho uma função para cálculo do CRC16 (últimos 4 dígitos da payload) do PIX, mas está em uma função do PostGreSQL.

Preciso colocar em um projeto bem antigo, em VB6 e não consegui converter (VB6 está lá nas lembranças mais longes... kkkkk)

Alguém tem conhecimento de um código em VB6 para calcular corretamente o CRC do PIX?

Converter para vb.Net consegui, mas o resultado não vem certo.

Grato.
SINCLAIR 27/07/2023 18:07:42
#501617
Prezados, consegui converter.

Segue o código em VB6, para quem precisar.

  Option Explicit

Private Function CRC16CCITT(ByVal pData As String, ByVal length As Long) As Long
Dim i As Long
Dim wCrc As Long
Dim j
wCrc = &HFFFF

Dim ch As Byte
For i = 1 To length
ch = Asc(Mid(pData, i, 1))
wCrc = wCrc Xor (CLng(ch) * &H100)
For j = 1 To 8
If wCrc And &H8000 Then
wCrc = ((wCrc And &H7FFF) * 2) Xor &H1021
Else
wCrc = (wCrc And &H7FFF) * 2
End If
Next j
Next i

CRC16CCITT = wCrc And &HFFFF
End Function

Public Function HB_CRC16_CCITT(ByVal szString As String) As Long
HB_CRC16_CCITT = CRC16CCITT(szString, Len(szString))
End Function

Public Function HB_NUMTOHEX(ByVal num As Long) As String
HB_NUMTOHEX = Right("0000" & Hex(num), 4)
End Function

Private Sub Command1_Click()
Dim qrc As String
Dim crc As Long

qrc = "00020126330014BR.GOV.BCB.PIX01115794666323452040000530398654041.005802BR5925CLENILTON CRUZ DE ALENCAR6006MANAUS62070503***6304"
crc = HB_CRC16_CCITT(qrc)

MsgBox "CRC:" & HB_NUMTOHEX(crc)
MsgBox "Final string:" & qrc & HB_NUMTOHEX(crc)
End Sub
Tópico encerrado , respostas não são mais permitidas