CRC16校验代码

function CRC16(Data: string): string;
var
  I, Len, CRC: Word;
  pData: PChar;
begin
  CRC := 0;
  Len := Length(Data);
  pData := PChar(Data);

  while Len > 0 do
  begin
    CRC := CRC xor (Word(pData^) shl 8);
    Inc(pData);
    I := 8;
    repeat
      if CRC and $8000 > 0 then
        CRC := (CRC shl 1) xor $1021
      else
        CRC := CRC shl 1;
      Dec(I);
    until I = 0;
    Dec(Len);
  end;
  Result := IntToHex(CRC, 4);
end;
/*****************************************************************************************
Function:    CalcCRC
Description: 计算CRC
Calls:       无
Called By:   无
Input:       数据串指针pui16_Data,数据长度ui16_lenth
Output:      无
Return:      16位CRC
Others:      无
Auther:       
Time:        2005-7-8
*****************************************************************************************/
UI16 CalcCRC(UI8 *pui16_Data, UI16 ui16_lenth)
{
    UI8 i;
    UI16 crc=0;
    UI16 lenth;
    
    lenth=ui16_lenth;
    while(lenth) 
    { 
        crc = crc ^ ((UI16)(*pui16_Data++) << 8);
        i=8;
        do
        { 
            if (crc & 0x8000) 
            {
                crc = (crc << 1) ^ 0x1021; 
            }
            else 
            {
                crc <<= 1; 
            }
        } while(--i);
        lenth--;
    } 
    return (crc); 
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值