首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C#中的CRC_CCITT Kermit 16

C#中的CRC_CCITT Kermit 16
EN

Stack Overflow用户
提问于 2011-10-05 17:34:59
回答 2查看 4.3K关注 0票数 1

我目前正在研究一种艰难的方法,它需要使用公式(X16 + X12 + X5 + 1)的CRC_CCITT kermit16协议。然而,我在网上找到的一些代码,无论是在这个网站上还是在网站上,我似乎没有得到我想要的结果。我看到这个网站(http://www.lammertbies.nl/comm/info/crc-calculation.html)实际上为我提供了我想要的完全匹配,但它是用C++编写的。有人能帮我吗?

我期待着您的回音。

亲切的问候

迈克尔

EN

回答 2

Stack Overflow用户

发布于 2012-11-07 15:43:32

如果您需要CRC CCITT 16 Kermit,您将需要以下代码(从我的网站):

代码语言:javascript
复制
var crc16Kermit = new Crc16( Crc16Mode.CcittKermit );
var checksum = crc16Kermit.ComputeChecksumBytes( 0x01, 0x23, 0x45 );
// checksum = 0x2e, 0x46

以下是上述代码的源代码

代码语言:javascript
复制
using System;

public enum Crc16Mode : ushort { Standard = 0xA001, CcittKermit = 0x8408 }

public class Crc16 {
    static ushort[] table = new ushort[256];

    public ushort ComputeChecksum( params byte[] bytes ) {
        ushort crc = 0;
        for(int i = 0; i < bytes.Length; ++i) {
            byte index = (byte)(crc ^ bytes[i]);
            crc = (ushort)((crc >> 8) ^ table[index]);
        }
        return crc;
    }

    public byte[] ComputeChecksumBytes( params byte[] bytes ) {
        ushort crc = ComputeChecksum( bytes );
        return BitConverter.GetBytes( crc );
    }

    public Crc16( Crc16Mode mode ) {
        ushort polynomial = (ushort)mode;
        ushort value;
        ushort temp;
        for(ushort i = 0; i < table.Length; ++i) {
            value = 0;
            temp = i;
            for(byte j = 0; j < 8; ++j) {
                if(((value ^ temp) & 0x0001) != 0) {
                    value = (ushort)((value >> 1) ^ polynomial);
                }else {
                    value >>= 1;
                }
                temp >>= 1;
            }
            table[i] = value;
        }
    }
}

上面代码的链接:http://sanity-free.org/147/standard_crc16_and_crc16_kermit_implementation_in_csharp.html

票数 3
EN

Stack Overflow用户

发布于 2011-10-05 17:38:07

看起来你想要CRC16 CCITT。试试这个:

代码语言:javascript
复制
using System;

public enum InitialCrcValue { Zeros, NonZero1 = 0xffff, NonZero2 = 0x1D0F }

public class Crc16Ccitt {
    const ushort poly = 4129;
    ushort[] table = new ushort[256];
    ushort initialValue = 0;

    public ushort ComputeChecksum(byte[] bytes) {
        ushort crc = this.initialValue;
        for(int i = 0; i < bytes.Length; ++i) {
            crc = (ushort)((crc << 8) ^ table[((crc >> 8) ^ (0xff & bytes[i]))]);
        }
        return crc;
    }

    public byte[] ComputeChecksumBytes(byte[] bytes) {
        ushort crc = ComputeChecksum(bytes);
        return BitConverter.GetBytes(crc);
    }

    public Crc16Ccitt(InitialCrcValue initialValue) {
        this.initialValue = (ushort)initialValue;
        ushort temp, a;
        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;
        }
    }
}

来源:http://sanity-free.org/133/crc_16_ccitt_in_csharp.html

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7659286

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档