www.pudn.com > codeprocess.rar > codeprocess.cpp


// codeprocess.cpp : Defines the entry point for the console application. 
// 
// 
 
//#include "stdafx.h" 
#include "stdio.h" 
 
short compress(char *); 
short encode(short); 
short decode(short); 
short decoder(short); 
int main(int argc, char* argv[]) 
{ 
//	int i; 
	short data,endata,endata1,dedata; 
	int i,input,output; 
	char temp[30]; 
	 
//  	for(;;) 
	{ 
	short temp1=0,temp2=0; 
	///////////////////---解交织----///////////////// 
	printf("plese input data .....\n"); 
    scanf("%d",&input); 
 
	for (i=0;i<15;i++) 
		{ 
			if ((input>>(2*i))&0x01) 
				temp1|=(0x01<>(2*i+1))&0x01) 
				temp2|=(0x01<>i)&0x01) 
				temp[i]='1'; 
			else 
				temp[i]='0';		    
		} 
	  /////////////////////////////////// 
	} 
		return 0; 
} 
 
short compress (char *x)//压缩函数 
{ 
	short temp = 0; 
	int i; 
	for (i=0;i<16;i++) 
	{ 
		if (x[i]=='1') 
			temp|=(((0x8000)>>(15-i))&0xffff); 
	} 
	return temp;	 
} 
 
 
short encode(short receive_data)//BCH编码 
{ 
	char temp[16]; 
	int j; 
	short endata; 
	for (j=0;j<11;j++) 
		{ 
			if ((receive_data>>j)&0x01) 
				temp[j]='1'; 
			else 
				temp[j]='0';		    
		} 
	if (((temp[0]+temp[1]+temp[2]+temp[3]+temp[5]+temp[7]+temp[8])%2)&0x01) 
		temp[11]='1'; 
	else 
		temp[11]='0'; 
	if (((temp[1]+temp[2]+temp[3]+temp[4]+temp[6]+temp[8]+temp[9])%2)&0x01) 
		temp[12]='1'; 
	else 
		temp[12]='0'; 
	if (((temp[2]+temp[3]+temp[4]+temp[5]+temp[7]+temp[9]+temp[10])%2)&0x01) 
	    temp[13]='1'; 
	else 
	    temp[13]='0'; 
	if(((temp[0]+temp[1]+temp[2]+temp[4]+temp[6]+temp[7]+temp[10])%2)&0x01) 
	    temp[14]='1'; 
	else 
	    temp[14]='0'; 
    endata = compress(temp); 
	return endata; 
} 
 
short decode (short receive_code)//BCH译码 
{ 
	short exp,correct_code,syndrome = 0; 
	char temp[16]; 
	int j; 
	temp[15]=0; 
	static char coef_table[15] ={9,13,15,14,7,10,5,11,12,6,3,8,4,2,1};//域元素的二进制表 
	for (j=0;j<15;j++)//接收码扩展 
		{ 
			if ((receive_code>>j)&0x01) 
				temp[j]='1'; 
			else 
				temp[j]='0';		    
		} 
	for (j=0;j<15;j++)//计算伴随式s=r(a) 
		{ 
			if (temp[j]=='1') 
			{ 
				syndrome^=coef_table[j]; 
			} 
		} 
	//printf("syndrome=%d\n",syndrome); 
	if (syndrome!=0) 
		{ 
			for (j=0;j<15;j++)//伴随式的指数 
				{ 
					if(syndrome==coef_table[j]) 
						exp=j; 
				} 
			printf("The wrong bit is %d\n",exp+1); 
			for (j=1;j<=15;j++) 
				{	 
					if (temp[exp]=='1') 
						temp[exp]='0'; 
					else temp[exp]='1'; 
				} 
			correct_code = compress(temp); 
			printf("The correct code is %d\n",correct_code); 
		} 
	else 
		{ 
			correct_code = receive_code; 
			printf("The code is write!!\n"); 
		} 
	return correct_code; 
} 
 
short decoder (short receive_code)//BCH译码 
{ 
	short exp,syndrome = 0; 
	char temp[16]; 
	int j; 
	temp[15]=0; 
	static char coef_table[15] ={9,13,15,14,7,10,5,11,12,6,3,8,4,2,1};//域元素的二进制表 
/*	for (j=0;j<15;j++)//接收码扩展 
		{ 
			if ((receive_code>>j)&0x01) 
				temp[j]='1'; 
			else 
				temp[j]='0';		    
		} 
*/ 
	for (j=0;j<15;j++)//计算伴随式s=r(a) 
		{ 
			if (((receive_code>>j)&0x01)) 
			{ 
				syndrome^=coef_table[j]; 
			} 
		} 
	//printf("syndrome=%d\n",syndrome); 
	if (syndrome!=0) 
		{ 
			for (j=0;j<15;j++)//伴随式的指数 
				{ 
					if(syndrome==coef_table[j]) 
						exp=j; 
				} 
			printf("The wrong bit is %d\n",exp+1); 
/*			for (j=1;j<=15;j++) 
				{	 
					if (temp[exp]=='1') 
						temp[exp]='0'; 
					else temp[exp]='1'; 
				} 
			correct_code = compress(temp); 
*/ 
            receive_code=receive_code^(0x01<