CRC16 para compor payload para o pix

Projeto [x]Harbour - Compilador de código aberto compatível com o Clipper.

Moderador: Moderadores

Avatar do usuário
juniorcamilo
Usuário Nível 3
Usuário Nível 3
Mensagens: 343
Registrado em: 10 Nov 2006 09:12
Localização: Pará

CRC16 para compor payload para o pix

Mensagem por juniorcamilo »

amigos

alguém tem essa função em xharbour?
Fernando queiroz
Usuário Nível 4
Usuário Nível 4
Mensagens: 779
Registrado em: 13 Nov 2014 00:41
Localização: Porto Alegre/RS

CRC16 para compor payload para o pix

Mensagem por Fernando queiroz »

amigo explica melhor , porque eu fiz a payload de PIX e não usei nada disso
HARBOUR 3.2, HWGUI 2.23 B3, SEFAZCLASS, PDFClass, ADO + MariaDB/MySQL, RMChart
Fernando queiroz
Usuário Nível 4
Usuário Nível 4
Mensagens: 779
Registrado em: 13 Nov 2014 00:41
Localização: Porto Alegre/RS

CRC16 para compor payload para o pix

Mensagem por Fernando queiroz »

o que voce precisa é somente isso

hb_numtohex(hb_crcct(cString, 0xFFFF, 0x11021))

Código: Selecionar todos

Martinello, thanks for the reply. In fact, I had already encountered these functions. I believe that I did not know how to elaborate my doubt.
I'm putting together a routine to generate the QRCode for PIX. The documentation about it guides that the last position of the string is the calculation of the CRC16 of the string in redundancy. That's where I'm encountering the difficulty.
I have a test string, and I can't get to the value of the field in question.
The QR string I'm assembling (in test) would be:

00020126330014BR.GOV.BCB.PIX01115794666323452040000530398654041.005802BR5925CLENILTON CRUZ DE ALENCAR6006MANAUS62070503***6304

And the checker Hexa code would be FA88

So the entire string would be

00020126330014BR.GOV.BCB.PIX01115794666323452040000530398654041.005802BR5925CLENILTON CRUZ DE ALENCAR6006MANAUS62070503***6304FA88

The question is: how to get to the FA88?
DEPOIS EU ENVIO O QRCODE ,
HARBOUR 3.2, HWGUI 2.23 B3, SEFAZCLASS, PDFClass, ADO + MariaDB/MySQL, RMChart
Avatar do usuário
sygecom
Administrador
Administrador
Mensagens: 7131
Registrado em: 21 Jul 2006 10:12
Localização: Alvorada-RS
Contato:

CRC16 para compor payload para o pix

Mensagem por sygecom »

Pelo que vi no xharbour tem a seguinte FUNCTION:

Código: Selecionar todos

   HB_CRC16(string) -> Crc16
      Calculates CCITT Crc16 (16-bit Cyclic redundancy checksum)
   Parameters:
      string  - string variable to calculate CRC16
   Returns:
      Crc16 as a 2-byte string variable (high byte first)

   HB_NCRC16(string,intermed16) -> nCrc16
      Calculates CCITT Crc16 (16-bit Cyclic redundancy checksum)
   Parameters:
      string     - string variable to calculate CRC16
      intermed16 - unnecessary parameter to start from default 0
                   as an integer (0-65535) if current string
                   is a fragment to calculate CRC32 for entire file
                   [HB_NCRC16(c1+c2,0)==HB_NCRC16(c2,HB_NCRC16(c1,0))]
   Returns:
      nCrc16 as an integer (0-65535)
Leonardo Machado
xHarbour.org + Hwgui + PostgreSql
Avatar do usuário
juniorcamilo
Usuário Nível 3
Usuário Nível 3
Mensagens: 343
Registrado em: 10 Nov 2006 09:12
Localização: Pará

CRC16 para compor payload para o pix

Mensagem por juniorcamilo »

Fernando queiroz escreveu:o que voce precisa é somente isso

hb_numtohex(hb_crcct(cString, 0xFFFF, 0x11021))
onde acho essa lib para essa Função para xHarbour123, para poder compilar junto a aplicação!
Fernando queiroz
Usuário Nível 4
Usuário Nível 4
Mensagens: 779
Registrado em: 13 Nov 2014 00:41
Localização: Porto Alegre/RS

CRC16 para compor payload para o pix

Mensagem por Fernando queiroz »

juniorcamilo escreveu:
Fernando queiroz escreveu:o que voce precisa é somente isso

hb_numtohex(hb_crcct(cString, 0xFFFF, 0x11021))
onde acho essa lib para essa Função para xHarbour123, para poder compilar junto a aplicação!
não uso XHARBOUR , em HARBOUR é uma função nativa , não precisa incluir nada
HARBOUR 3.2, HWGUI 2.23 B3, SEFAZCLASS, PDFClass, ADO + MariaDB/MySQL, RMChart
alxsts
Colaborador
Colaborador
Mensagens: 3092
Registrado em: 12 Ago 2008 15:50
Localização: São Paulo-SP-Brasil

CRC16 para compor payload para o pix

Mensagem por alxsts »

Olá!

NumToHex()

Converts a numeric value or a pointer to a Hex string.

Syntax

NumToHex( <nNum>|<pPointer>, [<nLen>]) --> cHexString

Arguments

<nNum>|<pPointer>
The first parameter is eiher a numeric value or a pointer to convert to hexadecimal notation.
<nLen>
An optional numeric value specifying the length of the return string. Return

The function returns a character string holding the passed value in hexadecimal notation.

Description

NumToHex() converts a numeric value or a pointer to a Hex formatted character string. It converts only integer values. If a number has a decimal fraction, it is ignored. The reverse function is HexToNum().
Info

See also: CStr(), HexToNum(), HexToStr(), StrToHex(), StrZero(), Transform()

Category: Numeric functions , Conversion functions , xHarbour extensions
Source: rtl\hbhex2n.c
LIB: xhb.lib
DLL: xhbdll.dll

Example

Código: Selecionar todos

// The example demonstrates return values of NumToHex()

   PROCEDURE Main()
     ? NumToHex( 1 )         // result: 1
     ? NumToHex( 128 )       // result: 80

     ? NumToHex( 1  , 4 )    // result: 0001
     ? NumToHex( 128, 4 )    // result: 0080

     ? NumToHex( -128 )      // result: FFFFFFFFFFFFFF80
     ? NumToHex( -1 )        // result: FFFFFFFFFFFFFFFF

     ? NumToHex( -1 , 4 )    // result: FFFF

     ? NumToHex( SQrt(2) )   // result: 1
   RETURN
Fonte: xHarbour Language Reference Guide
[]´s
Alexandre Santos (AlxSts)
Avatar do usuário
juniorcamilo
Usuário Nível 3
Usuário Nível 3
Mensagens: 343
Registrado em: 10 Nov 2006 09:12
Localização: Pará

CRC16 para compor payload para o pix

Mensagem por juniorcamilo »

Código: Selecionar todos

  #pragma begindump
/*
   Harbour CRC-16/CCITT-FALSE checksum implementaion
   Pete D. - Jun 2022
*/
#include "hbapi.h"
#include "hbapierr.h"
HB_U16 crc16ccitt( const char * pData, HB_SIZE length)
/* slightly modified code found here: https://gist.github.com/tijnkooijmans/10981093 */
{
    HB_U8 i;
   HB_U16 wCrc = 0xffff;
   while (length--)
    {
        wCrc ^= *(unsigned char *)pData++ << 8;
      for (i=0; i < 8; i++)
            wCrc = wCrc & 0x8000 ? (wCrc << 1) ^ 0x1021 : wCrc << 1;
    }
   return wCrc & 0xffff;
}

HB_FUNC( HB_CRC16_CCITT )
{
   const char * szString = hb_parc( 1 );

   if( szString )
   {
        hb_retnint( crc16ccitt( szString, hb_parclen( 1 ) ) );
   }
   else
      hb_errRT_BASE( EG_ARG, 3012, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS );
}

#pragma enddump

ta i amigos...
cleitonLC
Usuário Nível 1
Usuário Nível 1
Mensagens: 44
Registrado em: 17 Ago 2021 14:38
Localização: Cariacica/ES

CRC16 para compor payload para o pix

Mensagem por cleitonLC »

Código: Selecionar todos

#include "hbextcdp.ch"

hb_cdpselect("UTF8EX")

/*
Para testar o brcode use o site:
https://pix.nascent.com.br/tools/pix-qr-decoder/
*/

Private cString

cString := ""
nResult := 0

qr_static() //Gera qrcode estático, poder ser usado várias vezes, é fixo
//qr_dynamic() //Gera qrcode dinâmico com url do PSP, para pagamentos únicos, só pode ser usado 1 vez


cCommand := 'qrencode -m 2 -l H -t utf8 ' + '"' + cString + '"'
cCommand := 'curl qrcode.show -d ' + '"' + cString + '"'
cStdOut := Space(1024)
cStdErr := Space(1024)

nResult := hb_processRun( cCommand, , @cStdOut, @cStdErr )

? cStdOut

//setcolor("n/w,n/w,n/w")
//qout(cStdOut)


function qr_static()
	Local nPIX_AMOUNT := 0.00 // Valor do PIX
	Local cPIX_KEY := "13948132798"  // Chave pix
	Local cPIX_RECEIVER := "Cleiton Leonel Creton" // Nome do Proprietário do PIX
	Local cPIX_CITY := "Cariacica" // Nome da Cidade do proprietário do PIX
	Local cPIX_ZIPCODE := "" // Cep da rua do proprietário do PIX
	Local cPIX_DESCRIPTION := "Doacao Livre / QRCODE - HARBOUR PIX" // Descrição do PIX
	Local cPIX_IDENTIFICATION := "PIXMELINUX0001" // Identificador do PIX
	brcode_generator(nPIX_AMOUNT, cPIX_KEY, cPIX_RECEIVER, cPIX_CITY, cPIX_ZIPCODE, cPIX_DESCRIPTION, cPIX_IDENTIFICATION)
	
	? cString

return


function qr_dynamic()
	Local nPIX_AMOUNT := 1.00 // Valor do PIX
	Local cPIX_RECEIVER := "Cleiton Leonel Creton" // Nome do Proprietário do PIX
	Local cPIX_CITY := "Cariacica" // Nome da Cidade do proprietário do PIX
	Local cPIX_URL := "api-pix.bancobs2.com.br/spi/v2/a737c1c0-71c0-4939-9436-e03706047c74" // Url do PSP
	brcode_generator(nPIX_AMOUNT, cPIX_RECEIVER, cPIX_CITY, cPIX_URL)
	
	? cString

return


function brcode_generator(nPIX_AMOUNT, cPIX_KEY, cPIX_RECEIVER, cPIX_CITY, cPIX_ZIPCODE, cPIX_DESCRIPTION, cPIX_IDENTIFICATION, cPIX_URL)

	cString += get_value('00', '01')
	cString += get_value('01', '11')
	cString += get_account_information(cPIX_KEY, cPIX_DESCRIPTION)
	cString += get_value('52', '0000')
	cString += get_value('53', '986')
	cString += get_value('54', alltrim(str(nPIX_AMOUNT)))
	cString += get_value('58', 'BR')
	cString += get_value('59', cPIX_RECEIVER)
	cString += get_value('60', cPIX_CITY)
	cString += if(cPIX_ZIPCODE != Nil, get_value('61', cPIX_ZIPCODE), "")
	cString += get_additional_data_field(cPIX_IDENTIFICATION)
	cString += "6304"
	cString += hb_numtohex(hb_crcct(cString, 0xFFFF, 0x11021))
	
return cString
	

function get_value(identify, value)
return trim(identify + strzero(len(alltrim(value)), 2) + value)


function get_account_information(key, description, url)
	Local base_pix := get_value('00', 'br.gov.bcb.pix')
	Local info_string := ''
	
	if key != Nil
		info_string += get_value('01', key)
	elseif url != Nil
		info_string += get_value('25', url)
	endif
	if description != Nil
		info_string += get_value('02', description)
	endif
	
return get_value('26', base_pix + info_string)


function get_additional_data_field(identification)
    if identification != Nil
        return get_value('62', get_value('05', identification))
    else
        return get_value('62', get_value('05', '***'))
    endif

return

Fiz esse código a um tempo atrás, se for útil...
Responder