CRC校验实验报告.docx
《CRC校验实验报告.docx》由会员分享,可在线阅读,更多相关《CRC校验实验报告.docx(9页珍藏版)》请在冰豆网上搜索。
CRC校验实验报告
实验三CRC校验
一、CRC校验码的基本原理
编码过程:
CRC校验码的编码方法是用待发送的二进制数据t(x)除以生成
多项式g(x),将最后的余数作为CRC校验码。
其实现步骤如下:
1设待发送的数据块是m位的二进制多项式t(x),生成多项式
为r阶的g(x)。
在数据块的末尾添加r个0,数据块的长度增
加到m+r位。
2用生成多项式g(x)去除,求得余数为阶数为r-1的二进制
多项式y(x)。
此二进制多项式y(x)就是t(x)经过生成多项式
g(x)编码的CRC校验码。
3将y(x)的尾部加上校验码,得到二进制多项式。
就是包含
了CRC校验码的待发送字符串。
解码过程:
从CRC的编码规则可以看出,CRC编码实际上是将代发送的m位
二进制多项式t(x)转换成了可以被g(x)除尽的m+r位二进制多项式
所以解码时可以用接收到的数据去除g(x),如果余数位零,则
表示传输过程没有错误;如果余数不为零,则在传输过程中肯定
存在错误。
许多CRC的硬件解码电路就是按这种方式进行检错的。
同时,可以看做是由t(x)和CRC校验码的组合,所以解码时将接
收到的二进制数据去掉尾部的r位数据,得到的就是原始数据。
解码过程示例:
运行结果:
附录(实现代码):
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
namespaceCRC
{
publicabstractclassChange
{
///
///字节数组转进制
///
///字节数组
///字节数组长度
publicstaticstringByteToHex(byte[]bytes,intb1)
{
stringreturnStr="";
if(bytes!
=null)
{
for(inti=0;i{
returnStr+=bytes[i].ToString("x2").ToUpper();
}}
returnreturnStr;
}
///
///16进制转字节数组
///
///16进制数
publicstaticbyte[]HexToByte(stringhexStr)
{
hexStr=hexStr.Replace("","");
if((hexStr.Length%2)!
=0)
hexStr+="";//空格
byte[]bytes=newbyte[hexStr.Length/2];
for(inti=0;i{
bytes[i]=Convert.ToByte(hexStr.Substring(i*2,2),16);}
returnbytes;}
///
///字符串转进制
///
///字符串
///
publicstaticstringStrToHex(stringstr)
{
if(str=="")return"";
byte[]bTemp=System.Text.Encoding.Default.GetBytes(str);
returnByteToHex(bTemp,bTemp.Length);
}
///
///16进制转字符串
///
///16进制
///
publicstaticstringHexToStr(stringstr)
{
byte[]bytes=newbyte[str.Length];
bytes=HexToByte(str);
returnEncoding.Default.GetString(bytes);
}
}
}
namespaceCRC
{
//哈夫曼树150
//结点类Node的定义如下:
publicclassNode
{
privateintweight;//结点权值
privateintlChild;//左孩子结点
privateintrChild;//右孩子结点
privateintparent;//父结点
//结点权值属性
publicintWeight
{
get
{
returnweight;
}
set
{
weight=value;
}
}
//左孩子结点属性
publicintLChild
{
get
{
returnlChild;
}
set
{
lChild=value;
}
}
//右孩子结点属性
publicintRChild
{
get
{returnrChild;}
set{rChild=value;}}
//父结点属性
publicintParent
{get
{returnparent;}
set{
parent=value;}}
//构造器
publicNode()
{
weight=0;lChild=-1;
rChild=-1;parent=-1;}
//构造器
publicNode(intw,intlc,intrc,intp)
{
weight=w;
lChild=lc;
rChild=rc;
parent=p;
}
}
publicclassHuffmanTree
{
privateListdata=newList();//结点数组
privateintleafNum;//叶子结点数目
//索引器
publicNodethis[intindex]
{
get
{
returndata[index];
}
set
{
data[index]=value;
}
}
//叶子结点数目属性publicintLeafNum
publicintLeafNum
{
get
{returnleafNum;
set
{leafNum=value;}}
//构造器
publicHuffmanTree()
{}
publicHuffmanTree(Listm_NumKind)
{
leafNum=m_NumKind.Count;
for(intj=0;j<2*m_NumKind.Count-1;j++)//n中字符共需要2n-1个节点
{
Nodedatabuff=newNode();
if(j{
databuff.Weight=m_NumKind[j].num;}
data.Add(databuff);//每创建一个节点将节点加入节点数组data当中}
}
publicListCreate()
{
intmax1,max2,tmp1,tmp2;
//处理n个叶子结点,建立哈夫曼树
for(inti=0;i{max1=max2=Int32.MaxValue;
tmp1=tmp2=0;
//在全部结点中找权值最小的两个结点
for(intj=0;j{if((data[j].Weight{max2=max1;
tmp2=tmp1;
tmp1=j;
max1=data[j].Weight;}
elseif((data[j].Weight{max2=data[j].Weight;
tmp2=j;}}
data[tmp1].Parent=this.leafNum+i;
data[tmp2].Parent=this.leafNum+i;
data[this.leafNum+i].Weight=data[tmp1].Weight+data[tmp2].Weight;
data[this.leafNum+i].LChild=tmp1;
data[this.leafNum+i].RChild=tmp2;}
returndata;}}
publicclassNumKindchar
{publiccharletter{get;set;}//字符
publicintnum{get;set;}//字符出现的次数
publicListhuffmancode{get;set;}//字符对应的huffman编码
}
publicclasscodeChar
{
publiccharnumChar{get;set;}
publicstringcodeCharater{get;set;}
}
}