ASCII 和二进制之间的转换器和压缩器

发布于 2024-09-24 04:32:52 字数 1630 浏览 5 评论 0原文

我正在尝试制作一个非常简单的转换器/压缩器;该程序应获取包含 4 种不同类型 ASCII 字符的文件,并将其作为二进制写入文件。该程序还应该读取二进制文件并将其转换为 ASCII 并在屏幕上打印出来。下面是我的代码,我无法真正获取 char/cstring。我必须进行哪些类型的改进才能使其发挥作用?

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

char compresser(char c);
char converter(char c);

int main(int argc, char **argv)
{
    char *c;
    FILE *If = fopen("A.txt", "r");
    FILE *Uf = fopen("B.txt", "rw");

    if(If == NULL || Uf == NULL) {
            printf("Could not open file");
    }

    if(argc < 4) {
        printf("Too few argument, must be 3\n");

    } else if(strcmp(argv[1], "p") == 0) {
        while((c = fgetc(If)) != EOF) {
            printf("%c", c);
        }

    } else if(strcmp(argv[1], "e") == 0) {
        while((c = fgetc(If)) != EOF) {
            fprintf(Uf, "%c\n", compresser(c));
        }

    } else if(strcmp(argv[1], "d") == 0) {
        while((c = fgetc(Uf)) != EOF) {
            printf("%c", converter(c));
        }

    } else {
        printf("Not a valid command\n");
    }
}

char compresser(char c)
{
        if(c == ' ') {
            return '00';
        } else if(c == ':') {
            return '01';
        } else if(c == '@') {
            return '10';
        } else if(c == '\n') {
            return '11';
        } else {
            return 'e';
        }
}

char converter(char c)
{
        if(c == '00') {
            return ' ';
        } else if(c == '01') {
            return ':';
        } else if(c == '10') {
            return '@';
        } else if(c == '11') {
        return '\n';
    } else {
        return 'e';
    }

}

I am trying to make a very easy converter/compressor; the program should take a file with 4 different types of ASCII characters and writ it out as binary to a file. The program should also read the binary file and convert it to ASCII and print it out on the screen. Under is my code, I can’t really get the char/cstring. What types of improvement must I do to get this to work?

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

char compresser(char c);
char converter(char c);

int main(int argc, char **argv)
{
    char *c;
    FILE *If = fopen("A.txt", "r");
    FILE *Uf = fopen("B.txt", "rw");

    if(If == NULL || Uf == NULL) {
            printf("Could not open file");
    }

    if(argc < 4) {
        printf("Too few argument, must be 3\n");

    } else if(strcmp(argv[1], "p") == 0) {
        while((c = fgetc(If)) != EOF) {
            printf("%c", c);
        }

    } else if(strcmp(argv[1], "e") == 0) {
        while((c = fgetc(If)) != EOF) {
            fprintf(Uf, "%c\n", compresser(c));
        }

    } else if(strcmp(argv[1], "d") == 0) {
        while((c = fgetc(Uf)) != EOF) {
            printf("%c", converter(c));
        }

    } else {
        printf("Not a valid command\n");
    }
}

char compresser(char c)
{
        if(c == ' ') {
            return '00';
        } else if(c == ':') {
            return '01';
        } else if(c == '@') {
            return '10';
        } else if(c == '\n') {
            return '11';
        } else {
            return 'e';
        }
}

char converter(char c)
{
        if(c == '00') {
            return ' ';
        } else if(c == '01') {
            return ':';
        } else if(c == '10') {
            return '@';
        } else if(c == '11') {
        return '\n';
    } else {
        return 'e';
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

つ可否回来 2024-10-01 04:32:52

您可能需要解决很多问题。

我注意到的第一件事是您测试错误条件,打印错误消息,然后继续处理,就好像一切正​​常一样。

第二个是您的压缩算法不是压缩算法。就目前情况而言,您似乎希望每个未压缩的字符变成压缩形式的两个字符。即使您想按照 Eli 的建议写入十进制值,也不会减少存储字符所需的空间。

我的猜测是,您真正想要的是 二进制,而不是十进制表示形式。这将允许您使用两位而不是完整字节来表示每个字符。

例如,给定四个字符“A”、“B”、“C”和“D”,一种可能的二进制表示形式是

CHAR    BITS
A   <=>   00
B   <=>   01
C   <=>   10
D   <=>   11

然后您可以选择如何将这些值组合成压缩形式,例如,四个字符序列 ABAD 可以表示为0001001111000100。只需确保您将它们分开的方式与您组合它们的方式相对应即可。

只是为了明确说明 00010011 相当于十进制 19

看看此处了解有关如何操作位的更多详细信息。

Well there's a number of issues that you might want to address.

The first thing I noticed is that you test for error conditions, print an error message, and then continue processing as if everything is OK.

The second is that your compression algorihm isn't a compression algorithm. As it stands, you seem to want each uncompressed character to become two in compressed form. Even if you instead want to write a decimal value as suggested by Eli, you're not reducing the space required to store the character.

My guess is that what you really want is binary, not decimal representation. That would alllow you to represent each character using two bits instead of a full byte.

For example, given four characters 'A', 'B', 'C' and 'D' one possible binary representation would be

CHAR    BITS
A   <=>   00
B   <=>   01
C   <=>   10
D   <=>   11

Then you can choose how to combine these values into compressed form for example the four character sequence ABAD could be represented as either 00010011 or 11000100. Just make sure that the you separate them in the way the corresponds to the way you combine them.

Just to make it clear 00010011 is equivalent to the decimal 19.

Have a look here for more details on how to manipulate bits.

紫轩蝶泪 2024-10-01 04:32:52

此代码不正确:

char compresser(char c)
{
        if(c == ' ') {
            return '00';
        } else if(c == ':') {
            return '01';
        } else if(c == '@') {
            return '10';
        } else if(c == '\n') {
            return '11';
        } else {
            return 'e';
        }
}

“00”在 C 中无效,因为字符文字必须由单个字符组成。 '\x00' 表示值为 0 的字符。使用 \x00 表示二进制 0x00,而不是 00

用于澄清的示例代码:

#include <stdio.h>

int main()
{
    char c = '\x61';

    printf("%c\n", c);

    return 0;
}

请参阅我如何定义 c


然而,话虽如此,我不明白你的方法打算如何压缩字符。

This code isn't correct:

char compresser(char c)
{
        if(c == ' ') {
            return '00';
        } else if(c == ':') {
            return '01';
        } else if(c == '@') {
            return '10';
        } else if(c == '\n') {
            return '11';
        } else {
            return 'e';
        }
}

'00' is invalid in C, because a character literal must consist of a single character. '\x00' means the character with the value 0. Use \x00 for a binary 0x00, not 00.

Example code for clarification:

#include <stdio.h>

int main()
{
    char c = '\x61';

    printf("%c\n", c);

    return 0;
}

See how I defined c.


That said, however, I don't see how your approach intends to compress the characters.

海风掠过北极光 2024-10-01 04:32:52

这是一个示例代码片段:

unsigned int Compress(char letter_1, char letter_2, char letter3, char letter 4)
{
  unsigned int value = 0;
  unsigned int result = 0;
  value = letter1 - 'A';
  result = result << 2; // Shift the old to make room for new bits.
  result |= value;      // Put in new bits.
  value = letter2 - 'A';
  result = result << 2; // Shift the old to make room for new bits.
  result |= value;      // Put in new bits.
  value = letter3 - 'A';
  result = result << 2; // Shift the old to make room for new bits.
  result |= value;      // Put in new bits.
  value = letter4 - 'A';
  result = result << 2; // Shift the old to make room for new bits.
  result |= value;      // Put in new bits.
  return result;
}

这是一个压缩字母 (letter - 'A') 并打包为 unsigned int 的示例 (result = result < ;<2;结果|=值;)。

可能有更有效或更紧凑的方法,但这仅用于演示目的。

Here is an example code fragment:

unsigned int Compress(char letter_1, char letter_2, char letter3, char letter 4)
{
  unsigned int value = 0;
  unsigned int result = 0;
  value = letter1 - 'A';
  result = result << 2; // Shift the old to make room for new bits.
  result |= value;      // Put in new bits.
  value = letter2 - 'A';
  result = result << 2; // Shift the old to make room for new bits.
  result |= value;      // Put in new bits.
  value = letter3 - 'A';
  result = result << 2; // Shift the old to make room for new bits.
  result |= value;      // Put in new bits.
  value = letter4 - 'A';
  result = result << 2; // Shift the old to make room for new bits.
  result |= value;      // Put in new bits.
  return result;
}

This is an example of compressing the letter (letter - 'A') and packing into an unsigned int (result = result << 2; result |= value;).

There may be more efficient or compact methods, but this is for demonstration purposes only.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文