码流后面加8个0可以用移位得到(码流<<8;)
单次异或运算可以用运算符:^(运算符两边为常数)
由于你校验的是5个字节,且要多次异或运算,所以得借助数组,或其它的数据结果才能完成。
最后问一下你是做硬件的吗
#include <stdio.h>#include <string.h>
#include "stdlib.h"
unsigned int char2int(char *str)
{
unsigned int count=0, ret=0
for(count = 0count<strlen(str)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=0c <strlen(str)c++)
{if(str[c] != '0')<br/> {return ret-c}
}
}
int getRi(unsigned int num)
{
int c =0
for(num != 0c++)
{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)
}