河北水利局项目
This commit is contained in:
47
yw/crc.c
Normal file
47
yw/crc.c
Normal file
@@ -0,0 +1,47 @@
|
||||
#include "stdafx.h"
|
||||
#include "crc.h"
|
||||
/*
|
||||
static u16 crc16(unsigned char *buf,unsigned short length)
|
||||
{
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
int crc16 = 0xffff;
|
||||
for (i = 0;i < length;i++)
|
||||
{
|
||||
crc16 = crc16 ^ buf[i];
|
||||
for (j = 0;j < 8;j++)
|
||||
{
|
||||
if (crc16 & 0x01)
|
||||
{
|
||||
crc16 = (crc16 >> 1) ^ 0xa001;
|
||||
}
|
||||
else
|
||||
{
|
||||
crc16 = crc16 >> 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return (u16)crc16;
|
||||
}
|
||||
*/
|
||||
|
||||
static unsigned int cal_crc16(unsigned char *ptr, unsigned int len)
|
||||
{
|
||||
unsigned int crc;
|
||||
unsigned char i;
|
||||
crc=0xffff;
|
||||
while(len--!=0) {
|
||||
crc = crc^(*ptr);
|
||||
for(i=0; i<8; i++) {
|
||||
if((crc&0x0001)==0x0001) {crc>>=1;crc^=0xA001;}
|
||||
else crc>>=1;
|
||||
}
|
||||
ptr++;
|
||||
}
|
||||
return(crc);
|
||||
}
|
||||
u16 get_crc(u8 * val,u16 len)
|
||||
{
|
||||
return cal_crc16(val,len);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user