在char中存储两位二进制文​​件

时间:2013-10-07 16:08:45

标签: c binary char bit

所以我正在开发一个程序,它接受一个字符文件并将它们变成一个二进制数字符的文件。然后我必须能够读取这些字符(二进制字符)并将它们转回到表示的字符中。

所以基本上这是对文件进行编码和解码。

所以我有一个包含四个字符的文件:'@','/ n',':',''。 (最后一个是空格)

所以我想这样做的原因是我有一堆ascii图片,我想存储在较小的文件中。

我被告知我可以使用unsigned char,将其设置为0,然后使用上述字符读取文件,并使用按位运算符将读取的值分配给unsigned char,然后读取每四个字符(因为每个字符是8个字符,然后这些字符可以变成2个字符并存储在一个字符中,因此一个字符中有四个字符)将每个数字附加(添加)到字符。

任何帮助赞赏!

我现在的代码是:

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

FILE *inputFile;
FILE *outputFile;

int encodeBinary[4] = {0x00, 0x01, 0x02, 0x03};
char encodeChars[4] = {':', '@', '\n', ' '};

//reads from a file and creates the encoded file
void encode(const char * inFile, const char * outFile)
{

    inputFile = fopen(inFile, "r");
    outputFile = fopen(outFile, "w");
    char lineBuffer[BUFSIZ];
    int size = 0;
    char temp = 0;

    if(inputFile == NULL)
    {
        perror("Error while opening file.\n");
        exit(EXIT_FAILURE);
    }

    while(fgets(lineBuffer, sizeof(lineBuffer), inputFile))
    {
        for(int i = 0; lineBuffer[i] != 0; i++)
        {

            //adds four different characters to a char before adding the character to the file
            if(size < 4) {
                if(lineBuffer[i] == encodeChars[0])
                {

                }
                else if(lineBuffer[i] == encodeChars[1])
                {

                }
                else if(lineBuffer[i] == encodeChars[2])
                {

                }
                else if(lineBuffer[i] == encodeChars[3])
                {

                }

                size++;
            } else {
                size = 0;
                temp = 0;

            }
        }
    }

    fclose(inputFile);
    fclose(outputFile);

}

如果有人能够提供一些示例作为如何将读入的位添加到temp char中,我们将非常感激。我不知道如何将数字添加到char中,以便以这种方式移动它们并以这样的方式添加以表示旧数字和新数字。我想我可以将数字向左移位3次,这样01就变成了0100.

4 个答案:

答案 0 :(得分:1)

您可以使用&lt;&lt;而不是使用位域。和&gt;&gt;运算符和按位&amp;和|直接操纵位,

编码,

unsigned char accum;

if(size>0) accum = encodeChars[0] << 6
if(size>1) accum |= encodeChars[0] << 4
if(size>2) accum |= encodeChars[0] << 2
if(size>3) accum |= encodeChars[0]
if(lineBuffer[i] accum

解码,

char array[4];
if(size>0) array[0] = decodeChars[ (accum >> 6) &0x3 ];
if(size>1) array[1] = decodeChars[ (accum >> 4) &0x3 ];
if(size>2) array[2] = decodeChars[ (accum >> 2) &0x3 ];
if(size>3) array[3] = decodeChars[ (accum) &0x3  ];

并阅读big-endian与little-endian以了解元素存储顺序。

答案 1 :(得分:0)

像其他人一样说这需要一点思考。你的角色有8位,可以分为4个2位字段。因此,您最初需要将8位字符中的所有位设置为零。文件中的每个字符都需要编码为0-3之间的值。读入字符后,您需要找到其编码并创建编码值的掩码。需要将掩码移动到8位字符内的空2bit字段并与其进行OR运算。

有很多特定于实现的内容可供您使用,但这是基本的想法。

答案 2 :(得分:0)

假设你有一个来自文件的输入字符,我们称之为input_char;

unsigned char input_char;

你定义:

typedef union
{
   struct
   {
      unsigned char first  :2;
      unsigned char second :2;
      unsigned char third  :2;
      unsigned char fourth :2;
   } two_bits;

   unsigned char byte;

} UNION_TWO_BITS_STORAGE;

UNION_TWO_BITS_STORAGE storage_char;

然后

storage_char.two_bits.first = (input_char & 0x03);

只有input_char的第0位和第1位才有意义,这才有效。否则,你需要正确的转换。

或者,你可以

unsigned char temp;

switch(input_char)
{
     case '@':
        temp = 0x00;
        break;

     case '/n':
        temp = 0x01;
        break;
etc...

storage_char.two_bits.first = (temp);

并且,当你将字符写回到destiation文件时,你可以写

 storage_char.byte

是的,它有点乱,你可以用更环的方式编写它。但这只是为了展示使用联盟的概念,这里

答案 3 :(得分:0)

使用您的代码查看ChuckCottril所说的内容的另一种方法是以下

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

    FILE *inputFile;
    FILE *outputFile;

    int encodeBinary[4] = {0x00, 0x01, 0x02, 0x03};
    char encodeChars[4] = {':', '@', '\n', ' '};


    //reads from a file and creates the encoded file
    void encode(const char * inFile, const char * outFile)
    {

        inputFile = fopen(inFile, "r");
        outputFile = fopen(outFile, "w");
        char lineBuffer[BUFSIZ];
        int size = 0;
        char temp = 0;
        char output_char;

        if(inputFile == NULL)
        {
            perror("Error while opening file.\n");
            exit(EXIT_FAILURE);
        }

        output_char = 0;

        while(fgets(lineBuffer, sizeof(lineBuffer), inputFile))
        {
            for(int i = 0; lineBuffer[i] != 0; i++)
            {

                //adds four different characters to a char before adding the character to the file
                if(size < 4) {
                    if(lineBuffer[i] == encodeChars[0])
                    {
                       temp = encodeBinary[0];
                    }
                    else if(lineBuffer[i] == encodeChars[1])
                    {
                       temp = encodeBinary[1];
                    }
                    else if(lineBuffer[i] == encodeChars[2])
                    {
                       temp = encodeBinary[2];
                    }
                    else if(lineBuffer[i] == encodeChars[3])
                    {
                       temp = encodeBinary[3];
                    }

                    output_char |= temp << (size*2);

                    size++;
                } else {

                    //PRINT TO FILE OUTPUT CHAR, or save to output buffer
                    size = 0;
                    temp = 0;
                    output_char = 0;

                }
            }
        }

        fclose(inputFile);
        fclose(outputFile);

    }

要注意一件事:你应该总是有4个字节的倍数的输入文件,否则解码会导致不同的文件!