模拟计算机网络中的零比特填充

学网络的时候,学到一个零比特填充法。是用来保证数据在网络中传输的时候有一个比较稳妥的方式不产生错误。
用零比特填充是因为我们要让7EH(01111110)这个16进制数据表示数据的结尾,正因为如此,所以我们不能让数据本身包含这个7E数据,因为这样的话,数据就会被认为是结束。我们采用的方法是只让连续的5个‘1’在一起,当有6个‘1’的时候,在第5个‘1’和第6个‘1’之间填充一个‘0’,下面笔者就这一个方法用代码的方式来实现。由于只是想实现这个基本功能,所以就不在文件末尾添加7EH结束标志了。。。。

 

一、填充(从终端发出,形成传输文件)

CODE:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 
 5 #define MAX 100000 // The maximum length of binary string
 6 
 7 int main()
 8 {
 9     FILE* fp;
10     fp = fopen("input.txt", "r");
11 
12     if (fp == NULL)
13     {
14         printf("Sorry, your file is NULL. I can not to open it...\n");
15         return 1;
16     }
17 
18     char temp, bin[MAX];
19     memset(bin, 0, sizeof(bin)); // initizal this array bin[] to sure finsh.
20     int i;
21     temp = fgetc(fp); // get file information
22     for (i = 0; temp != EOF; ++i)
23     {
24         bin[i] = temp;
25         temp = fgetc(fp);
26     }
27 
28     int len = strlen(bin);
29 
30     printf("The original characters of the flow:");
31     for (i = 0; i < len; ++i)
32     {
33         printf("%c", bin[i]);
34     }
35     printf("\nbin len = %d\n", len);
36 
37 
38 
39     /* 0 bit filling */
40     char aux[MAX]; // auxiliary array
41     memset(aux, 0, sizeof(aux));
42     int m, flag; // flag is used to record the number of "1" states    
43     for (i = 0, m = 0, flag = 0; i < len; ++m)
44     {
45         if (bin[i] == '0')
46         {
47             flag = 0; // Flag to reset
48             aux[m] = bin[i];
49             ++i;
50         }
51         else
52         {
53             ++flag; // flag states chanage
54             if (flag != 6)
55             {
56                 aux[m] = bin[i];
57                 ++i;
58             }
59             else
60             {
61                 aux[m] = '0';
62                 flag = 0;
63             }
64         }
65     }
66 
67     len = strlen(aux);
68     printf("After filling the character of flow:");
69     for (i = 0; i < len; ++i)
70     {
71         printf("%c", aux[i]);
72     }
73     printf("\nbin len = %d\n", len);
74 
75     fclose(fp);
76 
77     return 0;
78 }

 input.txt文件可以如下:

1011010111001111111011111101111100001111101110

 

二、解除(到达终端,解密成信息文件)

CODE:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 
 5 #define MAX 100000 // The maximum length of binary string
 6 
 7 int main()
 8 {
 9     FILE* fp;
10     fp = fopen("input.txt", "r");
11 
12     if (fp == NULL)
13     {
14         printf("Sorry, your file is NULL. I can not to open it...\n");
15         return 1;
16     }
17 
18     char temp, bin[MAX];
19     memset(bin, 0, sizeof(bin)); // initizal this array bin[] to sure finsh.
20     int i;
21     temp = fgetc(fp); // get file information
22     for (i = 0; temp != EOF; ++i)
23     {
24         bin[i] = temp;
25         temp = fgetc(fp);
26     }
27 
28     int len = strlen(bin);
29 
30     printf("The original characters of the flow:");
31     for (i = 0; i < len; ++i)
32     {
33         printf("%c", bin[i]);
34     }
35     printf("\nbin len = %d\n", len);
36 
37 
38 
39     /* 0 bit filling */
40     char aux[MAX]; // auxiliary array
41     memset(aux, 0, sizeof(aux));
42     int m, flag; // flag is used to record the number of "1" states    
43     for (i = 0, m = 0, flag = 0; i < len; ++i)
44     {
45         
46         if (flag != 5)
47         {
48             aux[m] = bin[i];
49             if (bin[i] == '1')
50             {
51                 ++flag;
52             }
53             else
54             {
55                 flag = 0;
56             }
57             ++m;
58         }
59         else
60         {            
61             flag = 0;
62         }
63 
64     }
65 
66     len = strlen(aux);
67     printf("After filling the character of flow:");
68     for (i = 0; i < len; ++i)
69     {
70         printf("%c", aux[i]);
71     }
72     printf("\nbin len = %d\n", len);
73 
74     fclose(fp);
75 
76     return 0;
77 }

 input.txt文件可以如下:

10110101110011111011011111010111110000011111001110

 当然你也可以把这两个程序写成一个程序,笔者只是为了让读者能更好地理解这个东西才用两个程序代码来实现。。。

 

你可能感兴趣的:(计算机网络)