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

概述:

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


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

CODE:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX 100000 // The maximum length of binary string

int main()
{
    FILE* fp;
    fp = fopen("input.txt", "r");

    if (fp == NULL)
    {
        printf("Sorry, your file is NULL. I can not to open it...\n");
        return 1;
    }

    char temp, bin[MAX];
    memset(bin, 0, sizeof(bin)); // initizal this array bin[] to sure finsh.
    int i;
    temp = fgetc(fp); // get file information
    for (i = 0; temp != EOF; ++i)
    {
        bin[i] = temp;
        temp = fgetc(fp);
    }

    int len = strlen(bin);

    printf("The original characters of the flow:");
    for (i = 0; i < len; ++i)
    {
        printf("%c", bin[i]);
    }
    printf("\nbin len = %d\n", len);



    /* 0 bit filling */
    char aux[MAX]; // auxiliary array
    memset(aux, 0, sizeof(aux));
    int m, flag; // flag is used to record the number of "1" states    
    for (i = 0, m = 0, flag = 0; i < len; ++m)
    {
        if (bin[i] == '0')
        {
            flag = 0; // Flag to reset
            aux[m] = bin[i];
            ++i;
        }
        else
        {
            ++flag; // flag states chanage
            if (flag != 6)
            {
                aux[m] = bin[i];
                ++i;
            }
            else
            {
                aux[m] = '0';
                flag = 0;
            }
        }
    }

    len = strlen(aux);
    printf("After filling the character of flow:");
    for (i = 0; i < len; ++i)
    {
        printf("%c", aux[i]);
    }
    printf("\nbin len = %d\n", len);

    fclose(fp);

    return 0;
}

input.txt文件可以如下:

1011010111001111111011111101111100001111101110


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

CODE:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX 100000 // The maximum length of binary string

int main()
{
    FILE* fp;
    fp = fopen("input.txt", "r");

    if (fp == NULL)
    {
        printf("Sorry, your file is NULL. I can not to open it...\n");
        return 1;
    }

    char temp, bin[MAX];
    memset(bin, 0, sizeof(bin)); // initizal this array bin[] to sure finsh.
    int i;
    temp = fgetc(fp); // get file information
    for (i = 0; temp != EOF; ++i)
    {
        bin[i] = temp;
        temp = fgetc(fp);
    }

    int len = strlen(bin);

    printf("The original characters of the flow:");
    for (i = 0; i < len; ++i)
    {
        printf("%c", bin[i]);
    }
    printf("\nbin len = %d\n", len);



    /* 0 bit filling */
    char aux[MAX]; // auxiliary array
    memset(aux, 0, sizeof(aux));
    int m, flag; // flag is used to record the number of "1" states    
    for (i = 0, m = 0, flag = 0; i < len; ++i)
    {
        
        if (flag != 5)
        {
            aux[m] = bin[i];
            if (bin[i] == '1')
            {
                ++flag;
            }
            else
            {
                flag = 0;
            }
            ++m;
        }
        else
        {            
            flag = 0;
        }

    }

    len = strlen(aux);
    printf("After filling the character of flow:");
    for (i = 0; i < len; ++i)
    {
        printf("%c", aux[i]);
    }
    printf("\nbin len = %d\n", len);

    fclose(fp);

    return 0;
}

input.txt文件可以如下:

10110101110011111011011111010111110000011111001110


结尾:

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

你可能感兴趣的:(算法,网络,零比特填充)