代码:
#include <stdio.h>
#define maxSize 1000
int output(int bin){
FILE *fp;
char enter = '\n';
fp = fopen("OUTHDB3YUANMA.txt","a");
if(fp == NULL){
printf("数据错误!");
return 0;
}
fprintf(fp,"%d",bin);
fclose(fp);
return 0;
}
void HDB3_Encoding(char arr[], int len)
{
unsigned int i = 0;
unsigned int cnt_0 = 0;
char last_b = 0;
char last_v = -1;
int a[maxSize];
for(i ; i < len; i++)
{
if(arr[i]=='0')
{
cnt_0++;
if(cnt_0 >= 4)
{
cnt_0 = 0;
a[i] = (last_v&0x80)?1:-1;
last_v = a[i];
if(last_v != last_b)
{
a[i-3] = last_v;
last_b = last_v;
}
}
else
{
a[i] = 0;
}
}
else
{
cnt_0 = 0;
if(!last_b)
{
a[i] = 1;
last_b = 1;
}
else
{
a[i] = (last_b&0x80)?1:-1;
last_b = a[i];
}
}
output(a[i]);
}
printf("\n");
}
void input()
{
FILE *fp;
char arr[maxSize];
int a[maxSize];
int index = 0;
int flag = 0;
int i = 0;
int temp = 0;
fp = fopen("HDB3YUANMA.txt", "r");
if (fp == NULL)
{
printf("数据错误!");
return;
}
while (1)
{
flag = fscanf(fp, "%c", &arr[index]);
printf("%c ", arr[index]);
if (flag == EOF)
{
break;
}
index++;
}
if (flag)
{
printf("HDB3编码后:");
HDB3_Encoding(arr,index);
}
printf("\n");
fclose(fp);
}
int main()
{
input();
return 0;
}
HDB3YUANMA.txt: 010111000011010110000011110101110000101110000001011000000000000111010
OUTHDB3YUANMA.txt: 010-11-10001-110-101-1000-101-11-1010-11-10001-101-11000-10010-110001000-10001-11-1010
上一篇: 【光纤通信】实验一、C语言实现AMI编码
更多相关文章: 【通信工程】信息类,电子类,电气工程,自动化,计算机,软件工程,机电,等相关专业 全套学习指导
|