Cálculo do CRC16 para QRCode PIX / Calculation of CRC16 for QRCode PIX

1,614 views
Skip to first unread message

Clenilton Alencar

unread,
Jun 26, 2022, 11:18:26 AM6/26/22
to Harbour Minigui
Hi, all!!!

Alguém sabe informar se existe uma função nativa harbour que calcule o CRC16 para QrCode PIX? Ou alguma outra forma de calcular? Estou com muita dificuldade de implementar esse campo. Agradeço a ajuda de todos.



Does anyone know if there is a native harbor function that calculates the CRC16 for QrCode PIX? Or some other way to calculate? I'm having a hard time implementing this field. I thank the help of all you.

Clenilton Alencar

Harbour Minigui

unread,
Jun 26, 2022, 12:33:44 PM6/26/22
to Harbour Minigui
Hi Clenilton
maybe this can help you.
  • hb_CRC(<cValue> [, <nStart> ] [, <nPolynomial>]) nCRC8
  • returns the checksum of a string using the CRC-8 algorithm.
  • hb_CRC16(<cValue> [, <nStart> ]) nCRC16
  • returns the checksum of a string using the CRC-16 algorithm.
  • hb_CRC32(<cValue> [, <nStart> ]) nCRC32
  • returns the checksum of a string using the CRC-32 algorithm.
  • hb_CRCCT(<cValue> [, <nStart> ] [, <nPolynomial>) nCRCCT
  • returns the checksum of a string using CRC-8?, clipper tools compatible? or what? (clarification needed...). Anyhow, Przemek suggests to not use it.
please see : https://github.com/Petewg/harbour-core/wiki/hb_C 

And thanks to Pete!

HTH
--
Pierpaolo Martinello [ Minigui Team ]
IW1CUY Ham Radio From Biella Italy
Linux User 177880

Pete

unread,
Jun 27, 2022, 4:52:17 AM6/27/22
to Harbour Minigui
On Sunday, 26 June 2022 at 19:33:44 UTC+3 Harbour Minigui wrote:
Hi Clenilton
maybe this can help you.
  • hb_CRC(<cValue> [, <nStart> ] [, <nPolynomial>]) nCRC8
  • returns the checksum of a string using the CRC-8 algorithm.
  • hb_CRC16(<cValue> [, <nStart> ]) nCRC16
  • returns the checksum of a string using the CRC-16 algorithm.
  • hb_CRC32(<cValue> [, <nStart> ]) nCRC32
  • returns the checksum of a string using the CRC-32 algorithm.
  • hb_CRCCT(<cValue> [, <nStart> ] [, <nPolynomial>) nCRCCT
  • returns the checksum of a string using CRC-8?, clipper tools compatible? or what? (clarification needed...). Anyhow, Przemek suggests to not use it.
please see : https://github.com/Petewg/harbour-core/wiki/hb_C 

And thanks to Pete!


Hello Pierpaolo!

Many thanks for mentioning my humble wiki but the grateful "thanks" and all the credits
for his  tremendously perfect work, must go to  Przemyslaw Czerpak, who is the creator
of these (and many-many other) harbour functions.

By the way, how are you doing? (here we've started burning...)
my greetings to Italia!

regards,
Pete

Clenilton Alencar

unread,
Jun 27, 2022, 5:29:45 PM6/27/22
to Harbour Minigui
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?


Clenilton Alencar
from Amazonas, Manaus, Brazil. 

--
Visit our website on https://www.hmgextended.com/ or https://www.hmgextended.org/
---
You received this message because you are subscribed to the Google Groups "Harbour Minigui" group.
To unsubscribe from this group and stop receiving emails from it, send an email to minigui-foru...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/minigui-forum/02a125f7-5818-4780-bf3f-67323800196en%40googlegroups.com.


--
Clenilton Cruz de Alencar

Pete

unread,
Jun 28, 2022, 3:20:34 AM6/28/22
to Harbour Minigui
H,
I'm not sure I've understood well your question but If your question is "how to calculate the CRC16 of a given `QRString`
and attach to it the calculated `hex` value,  follow the steps below.

nCRC16     := hb_crc16( QRString )
hexCRC16 := hb_NumToHex( nCRC16 )
finalString := QRString + hexCRC16

regards,
Pete


Clenilton Alencar

unread,
Jun 28, 2022, 7:34:22 PM6/28/22
to Harbour Minigui
Returns an incorrect hexa code.

Pete

unread,
Jun 29, 2022, 6:21:30 AM6/29/22
to Harbour Minigui
Hi,

On Wednesday, 29 June 2022 at 02:34:22 UTC+3 clenil...@gmail.com wrote:
Returns an incorrect hexa code.


Yes, it returns incorrect code because the algorithm used is improper for your case!
In fact you need `CRC-16/CCITT-FALSE` which, as far asI know, is not available in harbour
but it's not difficult to implement it, thanks to the excellent C-API of Harbour.
See code below:
---------------------------------------------------------------------------------------------------------------------------------------------------------
proc main()
    local qrc := "00020126330014BR.GOV.BCB.PIX01115794666323452040000530398654041.005802BR5925CLENILTON CRUZ DE ALENCAR6006MANAUS62070503***6304"
    local crc := hb_numTohex( hb_CRC16_CCITT( qrc ) )
   
    cls
    ? "CRC:", crc
    ? "Final string:", qrc + crc

    return

#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

---------------------------------------------------------------------------------------------------------------------------------------------------------

regards,
Pete



 

Clenilton Alencar

unread,
Jun 29, 2022, 9:13:07 AM6/29/22
to Harbour Minigui
Pete, good morning!
Perfect, friend. The checksum is now correct.
Thank you very much!

Wanes Peres

unread,
Jul 12, 2022, 8:09:32 PM7/12/22
to Harbour Minigui
Boa noite,

Utilizei o exemplo acima, porem o CRC16 para QRCode PIX retornou invalido.
Alguem poderia me ajudar?

Retorna este:
"00020126440014br.gov.bcb.pix0120.5568999480090520400005303986540512.505802BR5911WANES PERES6010RIO BRANCO62070503***6304F51A"

Porem o aceitavel seria este:
"00020126360014br.gov.bcb.pix0114+5568999480090520400005303986540512.505802BR5911WANES PERES6010RIO BRANCO62070503***63045294"

Chamada: hb_numTohex( hb_CRC16_CCITT( vQR_Code ) )

///------
///---

#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

Pete

unread,
Jul 13, 2022, 12:55:51 PM7/13/22
to Harbour Minigui
Hi,

From what I can see, you've posted two different QRCodes.
And both CRC results as returned by the function are perfectly correct!
you can check them here: https://crccalc.com/

regards,
Pete
Reply all
Reply to author
Forward
0 new messages