📄 code-decode.cpp
字号:
#include "stdio.h"
void main()
{
int code_in;
int i,j,a;
int code_out[15];
int mid[15];
/* code */
int g[15]={0x1,0x2,0x4,0x8,0x10,0x20,0x40,0x80,0x100,0x200,0x365,0x1AF,0x35E,0x1D9,0x3B2};
printf("输入原始信息数据(16进制):");
scanf("%x",&code_in);
for(i=0;i<15;i++)
{
a=code_in&g[i];
for(j=0;j<10;j++)
{
mid[j]=a&1;
a=a>>1;
}
code_out[i]=mid[0];
for(j=1;j<10;j++)
code_out[i]=code_out[i]^mid[j];
}
printf("纠错编码后的输出比特流: ");
for(i=15;i>0;i--)
printf("%d",code_out[i-1]);
/* decode and correct */
for (i=0;i<15;i++)
{
code_out[i]=0;
mid[i]=0;
}
code_in=0;
i=0;
j=0;
a=0;
int check_out[5];
int errorflag,matchbit,matchflag,comparebit;
int check[5]={0x765,0x9AF,0x135E,0x21D9,0x43B2}; /*the check array*/
printf("\n输入接受端收到的原始数据(16进制):");
scanf("%x",&code_in); /*input the data received at the receiver*/
a=code_in;
for (i=0;i<15;i++) /*change the data received into bits*/
{
code_out[i]=a&1;
a=a>>1;
}
for(i=0;i<5;i++) /*caculate the cheksum*/
{
a=code_in&check[i];
for(j=0;j<15;j++) /*get every bit of a */
{
mid[j]=a&1;
a=a>>1;
}
check_out[i]=mid[0];
for(j=1;j<15;j++)
check_out[i]=check_out[i]^mid[j]; /*add the bits in mode 2*/
}
i=0;
errorflag=0;
do
{
if (check_out[i]==1)
{
errorflag=1;
i=5;
}
else
i++;
}
while (i<5); /* check if the check is all 0 */
if (errorflag==1)
{
i=0;
matchbit=100; /* look for the same line*/
do
{
j=0;
matchflag=1;
do
{
comparebit=(check[j]>>i)&1; /* get the [j][i] bit of the array*/
if (check_out[j]!=comparebit)
{
j=5;
matchflag=0;
} /*not coincident,jump out at once*/
else
j++;
}
while (j<5);
if (matchflag==1)
{
matchbit=i;
i=15;
} /*if found,jump out at once*/
else
i++;
}
while (i<15);
if (matchbit<=14)
{
code_out[matchbit]=code_out[matchbit]^1; /*correct the error bit*/
printf("第%d位数据出错!\n",matchbit+1);
}
else
printf("错误但不可纠!\n");
}
else printf("传输正确或错误不可检!\n");
printf("还原的信息比特为:");
for (i=10;i>0;i--) /*only print the information bits*/
printf("%d",code_out[i-1]);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -