简体   繁体   English

用c编码和解码文本

[英]Encoding and Decoding text in c

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

FILE *inputFile;
FILE *outputFile;

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

void encode(const char * inFile, const char * outFile)
{

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

    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++)
        {
            if(lineBuffer[i] == encodeChars[0])
            {
                fprintf(outputFile, "%d", encodeBinary[0]);
            }
            else if(lineBuffer[i] == encodeChars[1])
            {
                fprintf(outputFile, "%d", encodeBinary[1]);
            }
            else if(lineBuffer[i] == encodeChars[2])
            {
                fprintf(outputFile, "%d", encodeBinary[2]);
            }
            else if(lineBuffer[i] == encodeChars[3])
            {
                fprintf(outputFile, "%d", encodeBinary[3]);
            }
        }
    }

    fclose(inputFile);
    fclose(outputFile);

}

void decode(const char * inFile, const char * outFile)
{

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

    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++)
        {
            if(lineBuffer[i] == '0')
            {
                fprintf(outputFile, "%c", encodeChars[0]);
            }
            else if(lineBuffer[i] == '1')
            {
                fprintf(outputFile, "%c", encodeChars[1]);
            }
            else if(lineBuffer[i] == '2')
            {
                fprintf(outputFile, "%c", encodeChars[2]);
            }
            else if(lineBuffer[i] == '3')
            {
                fprintf(outputFile, "%c", encodeChars[3]);
            }
        }
    }

    fclose(inputFile);
    fclose(outputFile);

}


void commands(const char * command, const char * inputFile, const char * outputFile)
{
    if(strcmp(command, "encode") == 0)
    {
        encode(inputFile, outputFile);
    }
    else if(strcmp(command, "decode") == 0)
    {
        decode(inputFile, outputFile);
    }
}

void testValues(int argc, const char * argv[])
{
    if(argc == 4)
    {
        commands(argv[1], argv[2], argv[3]);
    }
    else
        printf("USAGE: ./encode [input_file] [output_file]\n");
}

//MAIN
int main(int argc, const char * argv[])
{

    testValues(argc, argv);

    return 0;
}

Hi there. 嗨,您好。 I have this piece of code. 我有这段代码。 The code is supposed to get a text file in consisting of the characters : @ "newline" and "space". 该代码应该获得包含以下字符的文本文件:@“ newline”和“ space”。 These characters are then supposed to be converted to binary, 0, 1, 10, 11. After that I also need a way to decode back to the original characters. 然后应该将这些字符转换为二进制0、1、10、11。之后,我还需要一种解码回原始字符的方法。 What I can't seem to figure out is how to be able to read difference between the numbers, if there is 001, how can I know that we are talking about 0, 01, and not 00, 1. I read somewhere that you can use bitwise operations to do this? 我似乎无法弄清楚的是如何读取数字之间的差异(如果有001),我怎么能知道我们在说的是0、01,而不是00、1。我读到了某个地方可以使用按位运算来做到这一点? Any help appreciated! 任何帮助表示赞赏!

So, I have change my code a bit. 因此,我对代码做了一些更改。 Now the problem is that when I store the values the file that is encoded is as large as the file that is to be encoded. 现在的问题是,当我存储值时,已编码的文件与要编码的文件一样大。 How can I store the values in a file in such a way that it stores the values as hexadecimal (or binary) so that the encoded file is smaller than the original file? 如何以一种将值存储为十六进制(或二进制)的方式将值存储在文件中,以使编码后的文件小于原始文件?

{0, 1, 10, 11}; are not binary numbers, they are decimal numbers, which is the default number format in C source code. 不是二进制数字,而是十进制数字,这是C源代码中的默认数字格式。 The other possible number bases are hex, written with a prefix 0x and octal, written with a prefix 0 . 其他可能的数字基数是十六进制的前缀0x和八进制的前缀0 There is no way to write binary numbers in standard C code (probably because they are considered hard to read for humans). 无法用标准C代码编写二进制数字(可能是因为人们难以理解二进制数字)。

So what you will have to do is to type the numbers in hex: 因此,您要做的是用十六进制键入数字:

{0x00, 0x01, 0x02, 0x03}

The algorithm is pretty straight-forward: 该算法非常简单:

  • Read a character from the file. 从文件中读取一个字符。
  • Search for a match of this character among encodeChars (which should be declared as const char [] ). encodeChars (应将其声明为const char [] )中搜索此字符的匹配项。
  • If found, replace it with the corresponding index in "binary". 如果找到,将其替换为“二进制”中的相应索引。
  • Decoding is the other way around, just use the binary as lookup table instead. 解码是另一种方式,只需使用二进制作为查找表即可。
  • If performance is important, consider implementing this using binary search. 如果性能很重要,请考虑使用二进制搜索来实现。 It is an ideal example of where binary search should be used (sorted data, no duplicates). 这是应在何处使用二进制搜索(排序的数据,没有重复项)的理想示例。

EDIT 编辑

What I spoke of is the representation of numbers for the programmer , inside the programmer's own source code. 我讲的是对程序员的数字表示,程序员自己的源代码内。 Here you can only use decimal, hex and octal. 在这里,您只能使用十进制,十六进制和八进制。

There is also the representation of numbers for the user , which I suppose is what you were looking for. 还有用户的数字表示形式,我想这就是您想要的。 This can be anything you fancy. 这可以是您喜欢的任何东西。

And finally there is the representation of numbers for the CPU . 最后是CPU的数字表示形式。 He only wants binary and nothing but binary. 他只想要二进制文件,只需要二进制文件。

Consider this: printf("%c", 0x41). 考虑一下: printf("%c", 0x41).

  • The programmer sees hex 41. 程序员看到的是十六进制41。
  • The user sees the letter A. 用户看到字母A。
  • The CPU sees something like "Store number 01000001 on the stack. Jump to subroutine." CPU看到类似“堆栈上的存储号01000001。跳转到子例程”的信息。

To display some random byte as a binary number to the user , simply do something like: 要显示一些随机字节的二进制数的用户 ,只需做一些事情,如:

#include <stdint.h>

uint8_t data = 0x41;

for(uint8_t i=0; i<8; i++)
{
  if( (data & (1<<i)) > 0)
  {
    printf("1");
  }
  else
  {
    printf("0");
  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM