注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Mr.Right

不顾一切的去想,于是我们有了梦想。脚踏实地的去做,于是梦想成了现实。

 
 
 

日志

 
 
关于我

人生一年又一年,只要每年都有所积累,有所成长,都有那么一次自己认为满意的花开时刻就好。即使一时不顺,也要敞开胸怀。生命的荣枯并不是简单的重复,一时的得失不是成败的尺度。花开不是荣耀,而是一个美丽的结束,花谢也不是耻辱,而是一个低调的开始。

网易考拉推荐

CRC8和CRC16校验C语言实现  

2015-10-31 00:36:26|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
即以此功德,庄严佛净土。上报四重恩,下济三途苦。惟愿见闻者,悉发菩提心。在世富贵全,往生极乐国。

#include <stdio.h>

// method 1
unsigned int crc8(unsigned int  message, unsigned char  POLYNOMIAL);
// method 2
/*
 * The width of the CRC calculation and result.
 * Modify the typedef for a 16 or 32-bit CRC standard.
 */


 typedef short int crc;  // crc16, POLYNOMIAL = 0x1021
 //typedef unsigned int crc; // POLYNOMIAL = 0x04C11DB7

#define WIDTH  (8 * sizeof(crc))
#define TOPBIT (1 << (WIDTH - 1))
crc crcSlow(unsigned char message[], int nBytes, unsigned int POLYNOMIAL);


int main(int argc, char *argv[])
{
unsigned int msg, yout, crcTruth;
unsigned int poly;
unsigned char ptr[] = {0x7E, 0x00, 0x05, 0x60, 0x31, 0x32, 0x33}; // used in CRC16
int arrayLength;

msg = 0x34EC; // 11 0100 1110 1100
poly = 0x0B; // 1011
yout = crc8(msg, poly); 
crcTruth = 4;
printf("0x%x, crc truth = 0x%x\n", yout, crcTruth);  // discard the left 32 bits 

printf("Hello, world\n");
msg = 0xE5; // 1110 0101
poly = 0xD8; // 1101 1000
yout = crc8(msg, poly); 
crcTruth = 4;
printf("0x%x, crc truth = 0x%x\n", yout, crcTruth); 


printf("CRC 16\n");
poly = 0x1021;
arrayLength = sizeof(ptr) / sizeof(ptr[0]);
yout = crcSlow(ptr, arrayLength, poly); 
crcTruth = 0x5B3E;
printf("0x%x, crc truth = 0x%x\n", yout, crcTruth); 

return 0;
}


unsigned int crc8(unsigned int  message, unsigned char  POLYNOMIAL)
{
    unsigned int  remainder;

    /*
     * Initially, the dividend is the remainder.
     */
    remainder = message;

    /*
     * For each bit position in the message....
     */
    for (unsigned int bit = 8; bit > 0; --bit) // bit = 8 since POLYNOMIAL occupies 8 bit, whose max value is 255
    {
        /*
         * If the uppermost bit is a 1...
         */
        if (remainder & 0x80)  // CRC 8, unsigned char
        {
            /*
             * XOR the previous remainder with the divisor.
             */
            remainder ^= POLYNOMIAL;
        }

        /*
         * Shift the next bit of the message into the remainder.
         */
        remainder = (remainder << 1); // remainder = remainder * 2
    }

    /*
     * Return only the relevant bits of the remainder as CRC.
     */
    remainder = (remainder >> 4); // remainder = remainder /16
return (remainder & 0xFF);  // Trim remainder to 8 bits
}   /* crc8() */




crc crcSlow(unsigned char message[], int nBytes, unsigned int POLYNOMIAL)
{
    crc  remainder = 0;

    /*
     * Perform modulo-2 division, a byte at a time.
     */
    for (int byte = 0; byte < nBytes; ++byte)
    {
        /*
         * Bring the next byte into the remainder.
         */
        remainder ^= (message[byte] << (WIDTH - 8));

        /*
         * Perform modulo-2 division, a bit at a time.
         */
        for (short int bit = 8; bit > 0; --bit)
        {
            /*
             * Try to divide the current data bit.
             */
            if (remainder & TOPBIT)
            {
                remainder = (remainder << 1) ^ POLYNOMIAL;
            }
            else
            {
                remainder = (remainder << 1);
            }
        }
    }

    /*
     * The final remainder is the CRC result.
     */
    return (remainder);

}   /* crcSlow() */


  评论这张
 
阅读(243)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2016