Page 1 of 2

FWH or harbour function support CRC? ( Solved )

PostPosted: Wed Jan 10, 2024 10:38 am
by richard-service
Hi

FWH or Harbour any CRC function?
I have some data below:
    CRC WIDTH : CRC-16
    Polynomial : 0x8005
    Initial Value : 0xFFFF
    Final Xor Value: 0x0000

input this 000107 -> CRC RESULT 0x8811

Re: FWH or harbour function support CRC?

PostPosted: Wed Jan 10, 2024 11:45 am
by Natter
nStrCRC(mystr)

Re: FWH or harbour function support CRC?

PostPosted: Thu Jan 11, 2024 5:14 am
by richard-service
Natter wrote:nStrCRC(mystr)


Yes, I try it. But not work.

Re: FWH or harbour function support CRC?

PostPosted: Thu Jan 11, 2024 9:59 am
by Antonio Linares
? hb_CStr( hb_SHA256( hb_memoRead( "filename.zip" ), nil ) )

Re: FWH or harbour function support CRC?

PostPosted: Fri Jan 12, 2024 6:16 am
by nageswaragunupudi
I tried this way but I am getting a different result.
Code: Select all  Expand view
#include "fivewin.ch"

function Main()

   local n

   ? n := CRC_16( "000107" )
   ? NUMTOHEX( n )   // "FF3A"

return nil

#pragma BEGINDUMP

#include <hbapi.h>

unsigned int crc16(unsigned char *data, unsigned int data_length ) {
    unsigned int crc = 0xFFFF;
    unsigned int polynomial = 0x8005;
    unsigned int i, j;

    for (  i = 0; i < data_length; i++) {
        crc ^= data[i];
        for ( j = 0; j < 8; j++) {
            crc = (crc >> 1) ^ ((crc & 1) ? polynomial : 0);
        }
    }
    return crc;
}

HB_FUNC( CRC_16 )
{
   hb_retni( crc16( hb_parc( 1 ), hb_parclen( 1 ) ) );
}

#pragma ENDDUMP

Re: FWH or harbour function support CRC?

PostPosted: Fri Jan 12, 2024 6:42 am
by richard-service
Antonio Linares wrote:? hb_CStr( hb_SHA256( hb_memoRead( "filename.zip" ), nil ) )


Dear Antonio,

It's not work.

Re: FWH or harbour function support CRC?

PostPosted: Fri Jan 12, 2024 6:44 am
by richard-service
nageswaragunupudi wrote:I tried this way but I am getting a different result.
Code: Select all  Expand view
#include "fivewin.ch"

function Main()

   local n

   ? n := CRC_16( "000107" )
   ? NUMTOHEX( n )   // "FF3A"

return nil

#pragma BEGINDUMP

#include <hbapi.h>

unsigned int crc16(unsigned char *data, unsigned int data_length ) {
    unsigned int crc = 0xFFFF;
    unsigned int polynomial = 0x8005;
    unsigned int i, j;

    for (  i = 0; i < data_length; i++) {
        crc ^= data[i];
        for ( j = 0; j < 8; j++) {
            crc = (crc >> 1) ^ ((crc & 1) ? polynomial : 0);
        }
    }
    return crc;
}

HB_FUNC( CRC_16 )
{
   hb_retni( crc16( hb_parc( 1 ), hb_parclen( 1 ) ) );
}

#pragma ENDDUMP


Dear Mr.Rao

Return value not work.

Re: FWH or harbour function support CRC?

PostPosted: Fri Jan 12, 2024 6:48 am
by richard-service
Dear Antonio, Mr.Rao, Natter

Right value => 0x8811

I show some test tools below:
Image

Re: FWH or harbour function support CRC?

PostPosted: Fri Jan 12, 2024 2:35 pm
by karinha
Code: Select all  Expand view

C:\HBBCC74\src\rtl

03/04/2020  19:58            12.184 hbcrc.c

C:\XHBBCC74\contrib\unicode

25/02/2020  13:19             6.613 hbcrc16.c
25/02/2020  13:19             8.003 hbcrc32.c
               2 arquivo(s)         14.616 bytes

C:\XHBBCC74\source\rtl

25/02/2020  13:20             3.116 hbcrc32.c
 


Regards, saludos.

Re: FWH or harbour function support CRC?

PostPosted: Tue Jan 16, 2024 9:49 am
by richard-service
karinha wrote:
Code: Select all  Expand view

C:\HBBCC74\src\rtl

03/04/2020  19:58            12.184 hbcrc.c

C:\XHBBCC74\contrib\unicode

25/02/2020  13:19             6.613 hbcrc16.c
25/02/2020  13:19             8.003 hbcrc32.c
               2 arquivo(s)         14.616 bytes

C:\XHBBCC74\source\rtl

25/02/2020  13:20             3.116 hbcrc32.c
 


Regards, Saludos.


Dear Karinha,

not work.
I have VS code from device. Could you test it?
If yes, please send to me or show Email.
Thank you.

Re: FWH or harbour function support CRC?

PostPosted: Tue Jan 16, 2024 12:09 pm
by karinha

Re: FWH or harbour function support CRC?

PostPosted: Thu Jan 18, 2024 2:36 am
by richard-service

Re: FWH or harbour function support CRC? ( Solved )

PostPosted: Tue Jan 23, 2024 8:24 am
by richard-service
Dear All,

FWH or harbour CRC function not work. Very strange.
so I make these code and solved my problem below:

Code: Select all  Expand view

#include "fivewin.ch"

MEMVAR aCrc
MEMVAR poly
MEMVAR initvalue
MEMVAR finavalue

FUNCTION MAIN()
LOCAL crcResu
PRIVATE aCrc:={0x00,0x01,0x07}
PRIVATE poly:=0x8005
PRIVATE initvalue:=0xFFFF
PRIVATE finavalue:=0x0000
Ccrcresu:=Calculate_Crc()
aa=hb_numtohex(crcresu,4)
RETURN Nil

FUNCTION Calculate_Crc()
LOCAL crc:=initvalue,i,j
FOR I=1 TO LEN(aCrc)
    crc =hb_bitxor(crc,hb_bitshift(aCrc[i],8))
    FOR j=1 to 8
        if hb_bitand(crc ,0x8000)<>0
           crc=hb_bitxor(hb_bitshift(crc,1), poly)
        else
           crc=hb_bitshift(crc,1)
        endif
    NEXT
NEXT
return hb_bitxor(crc,finavalue)
 

Re: FWH or harbour function support CRC? ( Solved )

PostPosted: Tue Jan 23, 2024 2:20 pm
by karinha
Code: Select all  Expand view

// C:\FWH\SAMPLES\CRC.PRG

#include "fivewin.ch"
// #include "HbCompat.ch"

MEMVAR aCrc, poly, initvalue, finavalue

FUNCTION Main()

   LOCAL crcResu, CCRCRESU, cResult

   PRIVATE aCrc := { 0x00, 0x01, 0x07 }
   PRIVATE poly := 0x8005
   PRIVATE initvalue := 0xFFFF
   PRIVATE finavalue := 0x0000

   Ccrcresu := Calculate_Crc()

   // asi, funciona:

   ? HB_NumToHex( Ccrcresu )

   ? HB_NumToHex( hb_Random( 0xFFFFFF ) ) + '"'

   // no funciona:
   // cResult := hb_NumToHex( crcresu, 4 ) // return error.

   // ? cResult

   // xHarbour:
   // cResult := NumToHex( crcresu, 4 )

RETURN NIL

FUNCTION Calculate_Crc()

   LOCAL crc := initvalue, i, j

   FOR I = 1 TO Len( aCrc )

      crc = hb_bitXor( crc, hb_bitShift( aCrc[ i ], 8 ) )

      FOR j = 1 TO 8

         IF hb_bitAnd( crc, 0x8000 ) <> 0

            crc = hb_bitXor( hb_bitShift( crc, 1 ), poly )

         ELSE

            crc = hb_bitShift( crc, 1 )

         ENDIF

      NEXT

   NEXT

RETURN hb_bitXor( crc, finavalue )

/*
hbmisc.hbc

   DecToHexa()
   HexaToDec()
   IsHexa()

hbnf.hbc (instalado):

   ft_Byt2Hex()
   ft_Hex2Dec()

xhb.hbc

   HexToNum()
   HexToStr()
   NumToHex()
   StrToHex()

Harbour Core ***

   hb_HexToNum()
   hb_HexToStr()
   hb_NumToHex()
   hb_StrToHex()

hbmisc.hbc

   BinToDec()
   DecToBin()
   IsBin()

hbnf.hbc

   ft_Dec2Bin()

Harbour Core ***  Return -> Int <- Param

   Bin2I()
   Bin2L()
   Bin2W()
   I2Bin()
   L2Bin()
*/


// FIN / END
 


Regards, saludos.

Re: FWH or harbour function support CRC? ( Solved )

PostPosted: Tue Jan 23, 2024 3:17 pm
by nageswaragunupudi
so I make these code and solved my problem below:

Congratulations and thanks for sharing your solution.