用C语言实现CRC编码程序
答案:1 悬赏:50 手机版
解决时间 2021-11-23 10:07
- 提问者网友:我是女神我骄傲
- 2021-11-22 15:27
用C语言实现CRC编码程序
最佳答案
- 五星知识达人网友:三千妖杀
- 2021-11-22 15:33
#include
#include
#include "stdlib.h"
unsigned int char2int(char *str)
{
unsigned int count=0, ret=0;
for(count = 0; count {
ret = ret<<1;
if('0' != str[count])
{ ret+=1;}
}
return ret;
}
unsigned int getR(char *str)
{
unsigned int c =0 ;
int ret = strlen(str)-1;
for(c=0;c < strlen(str);c++)
{if(str[c] != '0')
{return ret-c;}
}
}
int getRi(unsigned int num)
{
int c =0;
for(;num != 0; c++)
{num = num>>1;}
return c;
}
void CRC(char *scode, char *p, char*g )
{
unsigned int iP = char2int(p);
unsigned int iG = char2int(g);
unsigned int r= getR(g);
unsigned int code = iP << r;
unsigned int yx = code;
for(;getRi(yx) >= getRi(iG);)
{ yx = yx ^ (iG<<(getRi(yx) - getRi(iG)));}
code += yx;
itoa(code,scode,2);
}
void main() //定义主函数
{
char data[8]="" , bds[8]="",code[16]="";
printf("数据:");
scanf("%s", data);
printf("表达式:");
scanf("%s", bds);
CRC(code,data,bds);
printf("编码:%s",code);
}
#include
#include "stdlib.h"
unsigned int char2int(char *str)
{
unsigned int count=0, ret=0;
for(count = 0; count
ret = ret<<1;
if('0' != str[count])
{ ret+=1;}
}
return ret;
}
unsigned int getR(char *str)
{
unsigned int c =0 ;
int ret = strlen(str)-1;
for(c=0;c < strlen(str);c++)
{if(str[c] != '0')
{return ret-c;}
}
}
int getRi(unsigned int num)
{
int c =0;
for(;num != 0; c++)
{num = num>>1;}
return c;
}
void CRC(char *scode, char *p, char*g )
{
unsigned int iP = char2int(p);
unsigned int iG = char2int(g);
unsigned int r= getR(g);
unsigned int code = iP << r;
unsigned int yx = code;
for(;getRi(yx) >= getRi(iG);)
{ yx = yx ^ (iG<<(getRi(yx) - getRi(iG)));}
code += yx;
itoa(code,scode,2);
}
void main() //定义主函数
{
char data[8]="" , bds[8]="",code[16]="";
printf("数据:");
scanf("%s", data);
printf("表达式:");
scanf("%s", bds);
CRC(code,data,bds);
printf("编码:%s",code);
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯