CRC校验码计算
Type
TcrcArr = array [0..255] of Word;
TcrcArrDW = array [0..255] of LongWord;
const
P_16: Word = $A001;
P_CCITT: Word = $1021;
P_DNP: Word = $A6BC;
P_KERMIT: Word = $8408;
P_SICK: Word = $8005;
P_32: LongWord = $EDB88320;
var
crc_tab16, crc_tabccitt, crc_tabdnp, crc_tabkermit: TcrcArr;
crc_tab32: TcrcArrDW;
function Calcu_crc_16(const initialCRC: Word; const DataStr: string): Word;
var
i: integer;
tmp, short_c, crc: Word;
aByte: Byte;
begin
crc := initialCRC;
for i := 1 to Length(DataStr) do
begin
aByte := ord(DataStr[i]);
short_c := ($00ff and aByte);
tmp := crc xor short_c;
crc := (crc shr 8) xor crc_tab16[ tmp and $ff ];
end;
result := crc;
end;
function Calcu_crc_sick(const DataStr: string): Word;
var
i: integer;
short_c, short_p, crc, high_byte, low_byte: Word;
aByte, prev_byte: Byte;
begin
crc := $0000;
prev_byte := 0;
for i := 1 to Length(DataStr) do
begin
aByte := ord(DataStr[i]);
short_c := ($00ff and aByte);
short_p := ($00ff and prev_byte) shl 8;
if (crc and $8000) = $8000 then crc := (crc shl 1) xor P_SICK
else crc := (crc shl 1);
crc := crc and $FFFF;
crc := crc xor (short_c or short_p);
prev_byte := aByte;
end;
low_byte := (crc and $ff00) shr 8;
high_byte := (crc and $00ff) shl 8;
result := low_byte or high_byte;
end;
function Calcu_crc_ccitt(const initialCRC: Word; const DataStr: string): Word;
var
i: integer;
tmp, short_c, crc: Word;
aByte: Byte;
begin
crc := initialCRC;
for i := 1 to Length(DataStr) do
begin
aByte := ord(DataStr[i]);
short_c := ($00ff and aByte);
tmp := (crc shr 8) xor short_c;
crc := (crc shl 8) xor crc_tabccitt[tmp];
end;
result := crc;
end;
评论