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
Natter wrote:nStrCRC(mystr)
#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
Antonio Linares wrote:? hb_CStr( hb_SHA256( hb_memoRead( "filename.zip" ), nil ) )
nageswaragunupudi wrote:I tried this way but I am getting a different result.
- Code: Select all Expand view RUN
#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
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
karinha wrote:
- Code: Select all Expand view RUN
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.
karinha wrote:See if it helps,
https://github.com/harbour/core/issues/242
https://www.mail-archive.com/harbour@harbour-project.org/msg03872.html
https://harbour.harbour-project.narkive.com/ESShRUiG/sf-net-svn-project-12006-trunk
https://forums.fivetechsupport.com/viewtopic.php?f=6&t=24695
https://forums.fivetechsupport.com/viewtopic.php?f=3&t=31639
Regards, Saludos.
#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)
// 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
so I make these code and solved my problem below:
Return to FiveWin for Harbour/xHarbour
Users browsing this forum: Google [Bot], Silvio.Falconi and 42 guests