用C语言实现CRC编码程序

用C语言实现CRC编码程序,第1张

#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)

}

while(len--!=0)

这句的len的值循环一次就减少1,先执行len!=0,再执行len--。

当len为0时退出循环。

for(i=0x80

i!=0

i/=2)

0x80是十六进制数,也即128

当i!=0时,执行循环体

然后i=i/2,即i值减半。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/yw/11090272.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-13
下一篇2023-05-13

发表评论

登录后才能评论

评论列表(0条)

    保存