0

I need to convert bytes into binary, I have a couple of functions which will output the byte representation of a file. Does anybody know how to do this effeciently? I know of several code extensive methods but I'm curious to see the ideas people here come up with.

I have this function which create a byte dump file:

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);
}

which is tested by function 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);
}

You need these header files to run this:

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

To compile this program you need gcc and the input file, output file, length of line, and size must be entered at the command line:

gcc main.c
./a [inputfile.txt] [outputfile.txt] [line length] [size]
mosawi
  • 1,283
  • 5
  • 25
  • 48

1 Answers1

1

You preformat the 256 possible bit patterns and then use those to generate the output for each byte that you read. That's a good technique; it will be close to as fast as it is reasonably possible to do the job.

The code at:

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

should be just:

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

Your use of feof() and ferror() is not precisely wrong, but it is sub-optimal.

If it was my output, I'd have a space between each byte's worth of data:

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

I wouldn't print a warning about the file not being a multiple of the line-length; you can have files that contain a prime number of bytes, and they're perfectly OK and not warning-worthy in my view.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278