where : ibrtses delphi
Delphi - crc-16
disclaimer
the source code of this page may not appear correctly in certain browsers
due to special characters. Have a look at the source of this HTML page
with notepad instead
crc over one byte
procedure ByteCrc(data:byte;var crc:word);
VAR i:BYTE;
BEGIN
FOR i:=0 TO 7 DO
BEGIN
IF ((data and $01)XOR(crc AND $0001)<>0) THEN
BEGIN
crc:=crc shr 1;
crc:= crc XOR $A001;
END
ELSE crc:=crc shr 1;
data:=data shr 1; // this line is not ELSE and executed anyway.
END;
END;
crc over the string
the crc as result is initialized as zero, then each
byte is processed.
function StringCrc(s:shortstring):word;
var len,i:integer;
begin
result:=0;
len:=length(s);
for i:=1 to len do bytecrc(ord(s[i]),result);
end;
usage
The data is filled into a shortstring as :
crcstring:shortstring:=chr(data[1])+..+chr(data[N]);
MyCrc:word:=StringCrc(crcstring);
notes
This crc is calculated as X^16+X^15+X^2+1 and can
protect 2^16 bits or 8192 bytes against single bit
errors. For longer datablocks, the crc-32 is to be
used.
This the slow implementation requiring little RAM.
There also exists a fast implementation requiring
more RAM to hold a table. One byte is processed in
a few assembly instructions.
Feedback is welcome
sponsored links
Delphi
home
last updated: 23.june.99
Copyright (99,2000) Ing.Büro R.Tschaggelar