#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
//#include <conio.h>
char main (void)
{
int *x;
int n=0;
x=(int*)malloc(sizeof(int));
while(1)
{
scanf("%x",&x[n]); printf("n=%d value=%x\n",n+1,x[n]);
n++;
x=(int*)realloc(x,sizeof(int)*(n+1));
unsigned short wCrc;
unsigned short wCrcHigh;
unsigned short wCrcLow;
int i=0,j=0;
int result;
//---------------------CRC-CCITT-------------------//
wCrc=0; //初始值=0
for (i=0;i<n;i++)
{
wCrc^=(x[i]<<=8); //将结果wCrc与数组中的第i个值进行异或
for(j=0; j<8; j++)
{
if((wCrc&0x8000)!=0) //判断异或后的结果首位是否为0
{
wCrc<<=1; //若首位=1,则左移一位,并与多项式进行异或
wCrc^=0x1021; //多项式=11021,按照要求,省略最左面的1
}
else wCrc<<=1; //若首位不等于1,直接左移一位
}
}
result=wCrc;
wCrcHigh=(int)wCrc/256;
wCrcLow=(int)wCrc%256;
cout<<"The CRC result is:"<<hex<<result<<endl;
cout<<"The CRC Hignbyte result is:"<<hex<<wCrcHigh<<endl;
cout<<"The CRC Lowbyte result is:"<<hex<<wCrcLow<<endl;
}
getchar();
return 0;
}
评论0