本帖最后由 e1ki0lp 于 2015-5-26 23:43 编辑
- #include <stdio.h>
- #define uchar unsigned char
- #define uint unsigned int
- uchar dat[] = { 0x28,0x4C,0x2D,0xB4,0x15,0x00,0x00 }; //测试数据
- uchar CheckCrc() // Dallas iButton test vector. CRC-8 x8+x5+x4+1
- {
- uchar crc = 0, i,j;
- for (i = 0; i < sizeof dat/sizeof dat[0] ; i++)
- {
- crc = crc ^ dat[i];
- for (j = 0; j < 8; j++)
- {
- if (crc & 0x01)
- crc = (crc >> 1) ^ 0x8C;
- else
- crc >>= 1;
- }
- }
- return crc;
- }
- int main()
- {
- printf("%x,",CheckCrc()); //输出校验码 16进制格式
- return 0;
- }
查表法优势很大么?感觉计算起来也不是很麻烦啊。
|