/**二进制CRC序列生成程序*/#includestdio.h#includestring.h#define LEN_DIVIDEND 30#define LEN_DIVISOR 30#define LEN_SEQUENCE LEN_DIVIDEND+LEN_DIVISORvoid CRC(char*,char*,char*);void MOD2_div(char*,char*,char*);void move(char *,int);i
/**二进制CRC序列生成程序*/
#include<stdio.h>
#include<string.h>
#define LEN_DIVIDEND 30
#define LEN_DIVISOR 30
#define LEN_SEQUENCE LEN_DIVIDEND+LEN_DIVISOR
void CRC(char*,char*,char*);
void MOD2_div(char*,char*,char*);
void move(char *,int);
int main(){
char dividend[LEN_DIVIDEND];//被除数
char divisor[LEN_DIVISOR];//除数
char sequence[LEN_SEQUENCE];//CRC序列
freopen("in.txt","r",stdin);
printf("请输入源序列:");
scanf("%s",dividend);printf("%s\n",dividend);
printf("请输入检验码:");
scanf("%s",divisor);printf("%s\n",divisor);
CRC(dividend,divisor,sequence);
return 0;
}
void move(char *FCS,int len){
int i;
char c=FCS[0];
for(i=0;i<len-1;i++){
FCS[i]=FCS[i+1];
}
FCS[i]=c;
}
void MOD2_div(char *dividend,char *divisor,char *FCS){
int len_dividend=strlen(dividend);
int len_divisor=strlen(divisor);
int len_FCS=len_divisor-1;
int i,j;
for(i=0;i<len_divisor;i++)
FCS[i]=dividend[i];
FCS[len_divisor]=0;
printf("dividend=%s\n",dividend);
printf("divisor=%s\n",divisor);
printf("------------\n");
for(i=len_divisor;i<len_dividend+1;i++){
if(FCS[0]=='0'){
move(FCS,len_divisor);
FCS[len_divisor-1]=dividend[i];
continue;
}
for(j=0;j<len_divisor;j++){
FCS[j]=(dividend[i-len_divisor+j]+divisor[j]-96)%2+48;
}
move(FCS,len_divisor);FCS[len_divisor-1]=dividend[i];
//FCS[j-1]=dividend[i];
printf("%s\n",FCS);
}
printf("------------\n");
FCS[len_divisor-1]=0;
}
void CRC(char *dividend,char *divisor,char *sequence){
char FCS[LEN_DIVISOR];
int len_FCS=strlen(divisor)-1;
int i;
strcpy(sequence,dividend);
for(i=0;i<len_FCS;i++){
strcat(dividend,"0");
}
MOD2_div(dividend,divisor,FCS);
strcat(sequence,FCS);
printf("FCS=%s\n",FCS);
printf("sequence=%s\n",sequence);
}
【本文由:湖北阿里云代理商 http://www.558idc.com/aliyun.html提供,感恩】