1,594
社区成员




crc := $ffff;
for j := Low(buf) to High(buf) do begin // 不需要长度变量 len
chr := buf[j] and $ff;
crc := crc xor chr;
for i := 0 to 7 do begin
if (crc and 01) <> 0 then
crc := (crc shr 1) xor $a001
else
crc := crc shr 1;
end;
crc := crc and $ffff; // 外循环
end;
Result := crc;
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TArray = array of byte; // 定义动态数组
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
function mjComCRC(buf: TArray): integer; // 公共函数
var
Form1: TForm1;
implementation
function mjComCRC(buf: TArray): integer; // 计算 crc
var
crc, chr, i, j: integer;
begin
crc := $ffff;
for j := Low(buf) to High(buf) do begin // 不需要长度变量 len
chr := buf[j] and $ff;
crc := crc xor chr;
for i := 0 to 7 do begin
if (crc and 01) <> 0 then
crc := (crc shr 1) xor $a001
else
crc := crc shr 1;
crc := crc and $ffff;
end;
end;
Result := crc;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
a: TArray;
n, i: integer;
begin
SetLength(a, 10); // 设置动态数组
for i := Low(a) to High(a) do
a[i] := i;
n := mjComCRC(a); // 测试
showmessage(inttostr(n));
end;
end.