日期:2014-05-18  浏览次数:21003 次

C# ?CRC校验值的计算
求~~~?
用C#.net编写的"CRC校验值的计算"的代码....

多谢多谢

------解决方案--------------------
使用SharpZipLib的CRC校验。开源的
------解决方案--------------------
这个下载只有单个C#文件,能处理CRC1到CRC32。我自己没用过。
http://www.coders4fun.com/wp-content/plugins/wp-downloadMonitor/download.php?id=2

------解决方案--------------------
网上应该有下载的
------解决方案--------------------
C# code

uint gen_crc(byte[] frame)
{
    int frame_len = frame.Length;

    byte c;
    uint treat, bcrc;
    uint wcrc = 0;

    for (int i = 0; i < frame_len; i++)
    {
        c = frame[i];

        for (int j = 0; j < 8; j++)
        {
            treat = c & 0x80U;
            c <<= 1;
            bcrc = (wcrc >> 8) & 0x80;
            wcrc <<= 1;
            if (treat != bcrc)
            {
                wcrc ^= 0x1021;
            }
        }
    }

    return wcrc;
}

------解决方案--------------------


02 30 31 30 32 34 38 31 4D E5 03

分析一下,是这段“30 31 30 32 34 38 31”运算后才是“4D E5”

类型该换成unsigned short
C/C++ code
unsigned short gen_crc(BYTE far *frame, short frame_len)
{
        BYTE c, treat, bcrc;
        unsigned short wcrc = 0;
        short i, j;

        for (i = 0; i < frame_len; i++)
        {
                c = frame[i];

                for (j = 0; j  < 8; j++)
                {
                        treat = c & 0x80;
                        c  <<= 1;
                        bcrc = (wcrc >> 8) & 0x80;
                        wcrc  <<= 1;
                        if (treat != bcrc)
                                wcrc ^= 0x1021;
                }
        }

        return wcrc;
}