作者在 2010-01-25 13:09:12 发布以下内容
小弟收集的。呵呵。
dymcrc32 单元 unit DymCrc32; interface uses Windows,SysUtils,Classes; var Table:Array[0..255] of DWORD; procedure GetCRC32File(FileName:string;var CRC32:DWORD); function GetCrc32Str(s: string; Seed: LongInt):string; implementation procedure MakeTable(); var i,j,Crc:integer; begin for i:=0 to 255 do begin Crc:=i; for j:=0 to 7 do begin if (Crc and 1)<>0 then Crc:=(Crc shr 1) xor $EDB88320 else Crc:=Crc shr 1; end; Table end; end; procedure GetCRC32File(FileName:string;var CRC32:DWORD); var F:file; BytesRead:DWORD; Buffer:array[1..65521] of Byte; i:Word; begin MakeTable(); FileMode :=0; CRC32 :=$ffffffff; {$I-} AssignFile(F,FileName); Reset(F,1); if IoResult = 0 then begin repeat BlockRead(F,Buffer,Sizeof(Buffer),BytesRead); for i := 1 to BytesRead do CRC32 := (CRC32 shr 8) xor Table[Buffer until BytesRead = 0; end; CloseFile(F); {$I+} CRC32 := not CRC32; end; function GetCrc32Str(s: string; Seed: LongInt):string; var Count: Integer; CrcVal: LongInt; begin MakeTable(); CrcVal := Seed; for Count := 1 to Length(s) do CrcVal := Table[Byte(CrcVal xor DWORD(Ord(s[Count])))] xor ((CrcVal shr 8) and $00FFFFFF); Result := IntToHex(not(CrcVal), 8); end; end. DymMain 单元 unit DymMain; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, DymCrc32, ComCtrls; type TForm1 = class(TForm) Button1: TButton; Edit1: TEdit; Button2: TButton; Edit2: TEdit; Label1: TLabel; Label2: TLabel; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); begin Edit1.Text:=GetCrc32Str('0123456789',8);//这里取指定字符串的CRC32校验值; end; procedure TForm1.Button2Click(Sender: TObject); var FileStr:String; crc: DWORD; begin FileStr:=Application.ExeName;//这里取指定的文件的crc32校验值; GetCRC32File(FileStr,crc); if crc<>0 then Edit2.Text:=PChar(IntToHex(crc,8)); end; end. |