// Bits_Testing.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string.h>
#define CRC_16_POLY 0x8005
#define WORD int
#define BYTE char
#include<stdio.h>
char crc_array[300]={0};//計算CRC校驗碼時用到的臨時數組
int calcByte(int crc, char b)
{
int i;
crc = crc ^ (int)b << 8;
for ( i = 0; i < 8; i++)
{
if ((crc & 0x8000) == 0x8000)
crc = crc << 1 ^ CRC_16_POLY;
else
crc = crc << 1;
}
return crc & 0xffff;
}
//count crc-16, length in byte
int CRC16(char *pBuffer, int length)
{
int wCRC16=0xFFFF;
int i;
if (( pBuffer==0 )||( length==0 ))
{
return 0;
}
for ( i = 0; i < length; i++)
{
wCRC16 = calcByte(wCRC16, pBuffer[i]);
}
return wCRC16;
}
int crc_pointer=0;
char i, j;
char input[]= {0x00, 0x00, 0x28, 0x24, 0x00, 0x01, 0x00, 0x00, 0x00, 0xb7, 0xb9, 0x68, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x05, 0x80, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
//char input[]= {0x00, 0x00, 0x28, 0x24, 0x00, 0x05, 0x00, 0x00, 0x00, 0xf4, 0xc2, 0x68, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x05, 0x80, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
char length = sizeof (input) / sizeof (input[0]);
int _tmain(int argc, _TCHAR* argv[])
{
int crc_value=0;
crc_pointer=0;
for(j=0;j<length;j++)//這裏是將要進行校驗的數據放到臨時數組中,進行校驗
{
crc_array[crc_pointer++]=input[j];
}
printf("校驗的數據長度pointer=%d\n",crc_pointer);
crc_value=CRC16(crc_array,crc_pointer);//開始校驗
printf("CRC值是%x\n",crc_value);//返回結果
getchar();
}
C語言 CRC 16 源碼 算法
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
libxml2剖析(2):編譯
我还在迷路
2018-08-27 20:04:11
libxml2 主要函數用法
我还在迷路
2018-08-27 20:04:11
windows 編譯libxml2 64位及32位都可以使用
我还在迷路
2018-08-27 20:04:11
socket編程 及select poll epoll示例
我还在迷路
2018-08-27 20:04:09
libxml2剖析(3):使用教程
我还在迷路
2018-08-27 20:04:09
const的思考
duchuanying
2018-08-27 19:55:25
深入理解C語言指針的奧祕
duchuanying
2018-08-27 19:55:25
C/C++ 程序設計員應聘常見面試試題深入剖析
duchuanying
2018-08-27 19:55:24
內核模式下獲取SSDT服務索引代碼
毛主席夸帅
2018-08-27 19:54:14
獲取Windows系統版本號以及位數(兼容Win8)
毛主席夸帅
2018-08-27 19:54:13
[Win32]獲取當前系統可用磁盤空間最大的盤符
毛主席夸帅
2018-08-27 19:54:02
在物理內存不足的情況下使用內存映射文件進行大內存操作
毛主席夸帅
2018-08-27 19:54:01
DirectDraw方式截取系統屏幕
毛主席夸帅
2018-08-27 19:53:51
GDI方式截取系統屏幕
毛主席夸帅
2018-08-27 19:53:50