如何用C#中的多项式1021计算CRC ccitt xmodem [英] How to calculate CRC ccitt xmodem with polynomial 1021 in C#

查看:363
本文介绍了如何用C#中的多项式1021计算CRC ccitt xmodem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何用C#中的多项式1021计算CRC ccitt xmodem 





我试过的:



  ushort  result = Crc16Ccitt(  * 233050003LED); 

private ushort Crc16Ccitt( string strInput)
{
byte [] bytes = Encoding.ASCII.GetBytes(strInput);
const ushort poly = 4129 ;
ushort [] table = new ushort [ 256 ];
ushort initialValue = 0xffff;
ushort temp,a;
ushort crc = initialValue;
for int i = 0 ; i < table.Length; ++ i)
{
temp = 0 ;
a =( ushort )(i< << 8 );
for int j = 0 ; j < 8 ; ++ j)
{
< span class =code-keyword> if
(((temp ^ a)& 0x8000)!= 0
temp =( ushort )((temp<< 1 )^ poly);
else
temp<< = 1 ;
a<< = 1 ;
}
table [i] = temp;
}
for int i = 0 ; i < bytes.Length; ++ i)
{
crc =( ushort )((crc<< 8 )^ table [((crc>> 8 )^(0xff& bytes [i]))]);
}
return crc;
}

解决方案

发布的代码正确计算 CRC-16-CCITT 初始值 0xFFFF

参见在线CRC计算和免费库 [ ^ ]和微控制器 - CRC16校验和: HCS08与Kermit vs. XMODEM - Stack Overflow [ ^ ]。



如果你需要 XModem 一个然后改变

Quote:

ushort initialValue = 0xffff;

to

  ushort  initialValue =  0 ; 


How to calculate CRC ccitt xmodem with polynomial 1021 in C# 



What I have tried:

ushort result = Crc16Ccitt("*233050003LED");

private ushort Crc16Ccitt(string strInput)
        {
            byte[] bytes= Encoding.ASCII.GetBytes(strInput);
            const ushort poly = 4129;
            ushort[] table = new ushort[256];
            ushort initialValue = 0xffff;
            ushort temp, a;
            ushort crc = initialValue;
            for (int i = 0; i < table.Length; ++i)
            {
                temp = 0;
                a = (ushort)(i << 8);
                for (int j = 0; j < 8; ++j)
                {
                    if (((temp ^ a) & 0x8000) != 0)
                        temp = (ushort)((temp << 1) ^ poly);
                    else
                        temp <<= 1;
                    a <<= 1;
                }
                table[i] = temp;
            }
            for (int i = 0; i < bytes.Length; ++i)
            {
                crc = (ushort)((crc << 8) ^ table[((crc >> 8) ^ (0xff & bytes[i]))]);
            }
            return crc;
        }

解决方案

The posted code correctly computes the CRC-16-CCITT with initial value 0xFFFF.
See On-line CRC calculation and free library[^] and microcontroller - CRC16 checksum: HCS08 vs. Kermit vs. XMODEM - Stack Overflow[^].

If you need the XModem one then change from

Quote:

ushort initialValue = 0xffff;

to

ushort initialValue = 0;


这篇关于如何用C#中的多项式1021计算CRC ccitt xmodem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆