C#完整版CRC-16校验算法 CRC-16/XMODEM x16+x15+x5+1

 public UInt16  Cal_crc16(byte[] data, int size)
        {

            UInt32 i = 0;
            UInt16 crc = 0;
            for (i = 0; i < size; i++)
            {
                crc = UpdateCRC16(crc, data[i]);
            }
            crc = UpdateCRC16(crc, 0);
            crc = UpdateCRC16(crc, 0);

            return (UInt16)(crc);
        }
        public UInt16  UpdateCRC16(UInt16  crcIn, byte  bytee)
        {
            UInt32  crc = crcIn;
            UInt32  ins =(UInt32)bytee | 0x100;

            do
            {
                crc <<= 1;
		       ins <<= 1;
                if ((ins & 0x100)==0x100 )
		       {
                    ++crc;
                }
                if ((crc & 0x10000)==0x10000)
                {
                    crc ^= 0x1021;
                }
            }
            while (!((ins&0x10000)==0x10000) );
            return (UInt16 )crc;
        }      

传入参数为:byte[] array = new byte[] {0xbb,0xbb,0x43,0x43};

 private void button1_Click(object sender, EventArgs e)
        {
            CRC16_Verification crc16 = new CRC16_Verification();
            byte[] array = new byte[] {0xbb,0xbb,0x43,0x43};
            UInt16 crc = 0;
            crc = crc16.Cal_crc16(array, array.Length);
            tbxRawData.AppendText(crc.ToString());
     
        }

输出结果为:C1 F8

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值