CRC32生成码表方法实现。。。。。

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 ksaiy 的 blog

  table:array[0..255] of dword;

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[i]:=crc;
    end;
end;

procedure getcrc32file(filename:string;var crc32:dword);
var
  f:file;
  bytesread:dword;
  buffer:array[1..65521] of byte;
  i:word;
begin
  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[i] xor (crc32 and $000000ff)];
      until bytesread = 0;
    end;
  closefile(f);
  {$i+}
  crc32 := not crc32;
end;
   
function getcrc32str(s: string; seed: longint):string;
var
  count: integer;
  crcval: longint;
begin
  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;

调用:
procedure tform1.button1click(sender: tobject);
begin
  maketable();
  edit1.text:=getcrc32str('11111111',8);//这里取指定字符串的crc32校验值;
end;

procedure tform1.button2click(sender: tobject);
var
  filestr:string;
  crc: dword; 
begin
  maketable();
  filestr:=application.exename;//这里取指定的文件的crc32校验值;
  getcrc32file(filestr,crc);
  if crc<>0 then
    edit2.text:=pchar(inttohex(crc,6));
end;

本文关键:CRC32生成码表方法实现。。。。。
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top