logo资料库

海明码和CRC校验的C语言实现.doc

第1页 / 共8页
第2页 / 共8页
第3页 / 共8页
第4页 / 共8页
第5页 / 共8页
第6页 / 共8页
第7页 / 共8页
第8页 / 共8页
资料共8页,全文预览结束
1.海明码
CRC校验
海明码和 CRC 校验的 C 语言实现 1.海明码 //code by zxf 2010.4.10 #include #include #include //N 代表待编码数据的上限位数 #define N 100 int HmLength(int k);//计算海明码校验位位数 void InCode(char *data,char *c,int k,int r);//计算海明码每个校验位的数值 void main() { int k=0,r=0,dnum=0,cnum=0; char data[N]; char c[N]; clrscr(); printf("Now please input the data you want to Incode:"); for(k=0;k
{ printf("%c",data[dnum]); dnum++; } } getch(); } /* *@func:计算校验码 *@param:data 待编码数据,c 校验码,k 数据长度,r 校验码位数 *code by zxf */ void InCode(char *data,char *c,int k,int r) { for(int i=0;i
int r=0,flag=1; while (flag!=0)//循环到 2^r-1>=r+K 时即得到校验码的位数 { int temp=0; temp=pow(2,r); temp=temp-1; flag=(temp-r-k<0); r++; } return r-1; } 程序演示: 计算 1100 计算 1000001
计算 10101010101010101010101010101010101010 CRC 校验 //code by zxf 2010.4.10 #include #include #include //N 代表待编码数据的上限位数 #define N 100 void InputBinary(char *data);//输入二进制 int DelZero(char *data);//去掉二进制字符串前面的零位
int Div(char *data,char *div);//进行一次模 2 除法 void main() { char datacpy[N]; char data[N]; char div[N]; char res[N]; int reslen=0; clrscr(); printf("now please input the data:"); InputBinary(data);//输入 M printf("\nnow please input the div:"); InputBinary(div);//输入除数 DelZero(data); DelZero(div);//取得有效的 M 和除数 strcpy(datacpy,data);//备份 M printf("\nThe effective data is:%s",data); printf("\nThe effective div is:%s",div); for(int i=0;i=strlen(div))//记录零商 { for(int j=0;j0;i--) {
data[i]=data[i-1]; } data[0]='0'; } //输出结果 printf("\n_______________________________________________________________"); printf("\nThe div result is:%s",res); printf("\nThe FCS is:%s",data); printf("\nThe data wish FCS is:%s%s",datacpy,data); getch(); } /* *@func:二进制输入 *@param:data 用于存储输入数据的字符串 *code by zxf */ void InputBinary(char *data) { for(int i=0;i
} return res; } /* *@func:进行一次模 2 除法 *@param:data 被除数, div 除数 *@return:返回 0:被除数小于除数,返回 1:成功进行了一次模 2 除法 *code by zxf */ int Div(char *data,char *div) { int res=0; if(strlen(data)
分享到:
收藏