【问题标题】:how to convert bytes to binary in C language [closed]如何在 C 语言中将字节转换为二进制 [关闭]
【发布时间】:2013-09-20 03:39:11
【问题描述】:

我需要将字节转换为二进制,我有几个函数可以输出文件的字节表示。有人知道如何有效地做到这一点吗?我知道几种代码扩展方法,但我很想看看这里的人们提出的想法。

我有这个创建字节转储文件的函数:

void bindump(char *infile, char *outfile, int line_length, int size) {

FILE *in, *out;
int c;
int i, j;
char patterns[256][9];

for (i = 0; i < 256; i++) {
    for (j = 0; j < 8; j++) {
        patterns[i][j] = ((i>>(7-j))&1)+'0';
    }
    patterns[i][8] = '\0';
}

if (line_length % 8 != 0)
    errx(1, "Line length %d is not a multiple of 8\n", line_length);
line_length /= 8;

if ((in = fopen(infile, "r")) == NULL)
    err(1, "could not open %s for reading", infile);
if ((out = fopen(outfile, "w")) == NULL)
    err(1, "could not open %s for writing", outfile);
i = 0;
while (1) {
    c = fgetc(in);
    if (feof(in)) break;
    if (ferror(in))
        err(1, "error reading from %s", infile);

    if (fputs(patterns[c], out) == EOF)
        err(1, "error writing to %s", outfile);

    if ((++i) % line_length == 0) {
        if (fputc('\n', out) == EOF)
            err(1, "error writing to %s", outfile);
    }
}

if (i % line_length != 0) {
    if (fputc('\n', out) == EOF)
        err(1, "error writing to %s", outfile);
    warn("file length not multiple of line length (%d bits)", line_length*8);
}

if (size != 0) {
    int remaining = size-i/line_length;
    if (remaining < 0)
        errx(1, "too long file, length is %d\n", i/line_length);
    if (remaining > 0) {
        for (i = 0; i < remaining; i++) {
            for (j = 0; j < line_length; j++)
                fputs("00000000", out);
            fputc('\n', out);
        }
    }
}

fclose(in);
fclose(out);
}

由main函数测试:

int main(int argc, char **argv)
{
int size;

if (argc < 4) {
    fprintf(stderr,
        "usage: %s INFILE OUTFILE LINE-LENGTH [SIZE]\n"
        "Writes each bit from INFILE as ASCII '1' or '0' in OUTFILE,\n"
        "with a newline after every LINE-LENGTH bits.\n",
        argv[0]);
    return 1;
}

size = (argc == 4) ? 0 : atoi(argv[4]);
bindump(argv[1], argv[2], atoi(argv[3]), size);
}

你需要这些头文件来运行它:

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

要编译这个程序,你需要 gcc 并且必须在命令行输入输入文件、输出文件、行长和大小:

gcc main.c
./a [inputfile.txt] [outputfile.txt] [line length] [size]

【问题讨论】:

    标签: c unix binary byte xxd


    【解决方案1】:

    您预先格式化 256 种可能的位模式,然后使用它们为您读取的每个字节生成输出。这是一个很好的技术;它会尽可能快地完成这项工作。

    代码在:

    while (1) {
        c = fgetc(in);
        if (feof(in)) break;
        if (ferror(in))
            err(1, "error reading from %s", infile);
        ...
    

    应该只是:

    while ((c = getc(in)) != EOF)
    {
        ...
    

    您对feof()ferror() 的使用并没有完全错误,但它是次优的。

    如果是我的输出,我会在每个字节的数据之间留一个空格:

    if (fputc((++i % line_length == 0) ? '\n' : ' ', out) == EOF)
        err(1, "error writing to %s", outfile);
    

    我不会打印关于文件不是行长倍数的警告;您可以拥有包含素数字节的文件,并且它们完全可以,在我看来并不值得警告。

    【讨论】:

      猜你喜欢
      • 2017-05-14
      • 2017-08-28
      • 2016-07-16
      • 1970-01-01
      • 2017-04-07
      • 2018-12-14
      • 1970-01-01
      • 2012-03-14
      • 1970-01-01
      相关资源
      最近更新 更多