欢迎来到传世资源网!
加载中...
正在加载,请耐心等待...
本站为收藏、学习站,如有侵权,请联系管理员删除!

CRC校验码计算

介绍 评论 失效链接反馈

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;

下载声明:

本站资源均有第三方用户自行上传分享推荐,非本站自制,仅供玩家做交流学习之用!切勿用于商业用途!游戏作品版权归原作者享有,如有版权问题,请附带版权证明至邮件,本平台将应您的要求删除。
相关推荐:

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复