熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Delphi編程 >> 正文

教你一招:CRC算法的實現

2013-11-23 17:29:19  來源: Delphi編程 
uses
  WindowsSysUtilsClasses;
  
  const //Crc
  Table:Array[] of DWORD=
  ( $ $ $EEEC $BA
  $DC $AFF $EA $EA
  $EDB $DCBA $EDEE $DD
  $BCB $EBCBD $EBD $BFD
  $DB $ABF $FB $BEDE
  $ADADD $DDDEEB $FDB $DC
  $C $BAC $FDFA $ACEC
  $CF $CD $FAFD $DDF
  $BEC $CE $DE $A
  $CED $BD $DDFD $AABB
  $BAFA $BC $DBBBCD $ACBCF
  $DCE $DFC $DCDDCF $ABDD
  $DAC $DEA $CD $BFD
  $BFB $BC $CFBA $BBDAF
  $BE $F $CCDB $BBE
  $FFC $C $CDAB $BDD
  $DC $DB $DBC $EFDA
  $B $BBF $FBFEA $EBD
  $CA $FF $AE $EE
  $FADBB $DDD $C $EC
  $BBF $CC $D $FE
  $CED $BAB $FC $FFC
  $BDC $BE $BBEBEA $FCBC
  $DDDDF $DAD $CDCF $FBDC
  $DB $ABCE $ABC $DBBE
  $ADFA $DDD $ADCD $DDFFB
  $EA $EDFC $AD $DABD
  $D $DE $AAACF $DDDCC
  $C $AA $BEB $CC
  $B $FB $BD $CEEF
  $EDEFE $DC $BD $CDAB
  $BD $EBD $BBDCB $CBACAD
  $EDB $ABFBB $BEC $BDA
  $EAD $DDAF $DB $DC
  $EB $B $DDAE $AAAA
  $EECFB $FFD $AAE $DEB
  $FF $AD $EF $CFE
  $FD $CB $C $EBE
  $FEDB $DBE $DAAA $DDACC
  $FBDFF $EBEEFF $BBE $BED
  $DDAE $ADE $DCC $FDFF
  $DBBF $ABC $FBDD $BB
  $DDBDA $AFABC $AF $A
  $DFEFC $ADF $EEEF $BE
  $CBBC $BCA $FDA $E
  $CCC $BBB $B $F
  $CBABBE $BBDB $BBA $CBA
  $CDFFA $BDCF $CDEB $BDEAED
  $BCB $ECF $AAC $DA
  $CA $EBEF $ $
  $BFA $EBA $BBBAE $CBB
  $DEB $EDBED $CDCEFB $BDBDF
  $DDD $FDE $DDBF $FDAE
  $BECD $FBB $FBE $B
  $AE $FFFA $BCA $BC
  $FEFF $FAE $BFFD $CCF
  $AAE $DDDEE $E $BC
  $A $DF $D $EEDB
  $AEDAA $DDADC $DFB $DBF
  $ABCAE $DEBBEC $BCFF $BFFE
  $BDBDFC $CABACA $B $BAA
  $BAD $CDD $DE $DBF
  $BAE $CAB $DB $AFB
  $BBBE $CCEA $ADFB $DEFD);
  
  procedure GetCRCFile(FileName:string;var CRC:DWORD);
  function GetCrcStr(s: string; Seed: LongInt):string;
  
  implementation
  
  procedure GetCRCFile(FileName:string;var CRC:DWORD);
  var
  F:file;
  BytesRead:DWORD;
  Buffer:array[] of Byte;
  i:Word;
  begin
  FileMode :=;
  CRC :=$ffffffff;
  {$I}
  AssignFile(FFileName);
  Reset(F);
  if IoResult = then
  begin
  repeat
  BlockRead(FBufferSizeof(Buffer)BytesRead);
  for i := to BytesRead do
  CRC := (CRC shr ) xor Table[Buffer[i] xor (CRC and $ff)];
  until BytesRead = ;
  end;
  CloseFile(F);
  {$I+}
  CRC := not CRC;
  end;
  
  function GetCrcStr(s: string; Seed: LongInt):string;
  var
  Count: Integer;
  CrcVal: LongInt;
  begin
  CrcVal := Seed;
  for Count := to Length(s) do
  CrcVal := Table[Byte(CrcVal xor DWORD(Ord(s[Count])))] xor ((CrcVal shr ) and $FFFFFF);
  Result := IntToHex(not(CrcVal) );
  end;
  
  
  end
  
  
  調用
  uses Crc;
  
  procedure TFormButtonClick(Sender: TObject);
  begin
  EditText:=GetCrcStr();//這裡取指定字符串的CRC校驗值;
  end;
  
  procedure TFormButtonClick(Sender: TObject);
  var
  FileStr:String;
  crc: DWORD;
  begin
  FileStr:=ApplicationExeName;//這裡取指定的文件的crc校驗值;
  GetCRCFile(FileStrcrc);
  if crc<> then
  EditText:=PChar(IntToHex(crc));
  end;
From:http://tw.wingwit.com/Article/program/Delphi/201311/24674.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.