-1

I could get this code from a forum and modify it to be an easy code for reading. This code read any binary file then print the binary output as a hexdecimal raw.

#include <stdio.h>

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

    unsigned char buf[1] = {0};
    size_t bytes = 0, i, readsz = sizeof buf;
    
    FILE *fp = fopen("system.bin", "rb");

    while ((bytes = fread (buf, sizeof *buf, readsz, fp)) == readsz) {
        for (i = 0; i < readsz; i++)
            printf ("%02x ", buf[i]);
    }

    return 0;
}

An example of the output:

F7 F7 3B AF F7 2B BF DF 5C CE 9C

I have two questions:

  1. As per my knowledge all binary files on your computer are saved as a binary format like this 10100100101 so why the output wasn't like 10100100101.

  2. I do not want to convert the hexadecimal to binary format, I just want to read the binary file as a binary format not hexadecimal format.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
Khaled
  • 1
  • 1
  • 3
    As you seem to know, the `%x` format is to print hexadecimal representation of the value. Why did you expect it to print anything else? Also, there is no standard `printf` format to print in binary representation, you need to do the conversion explicitly. – Some programmer dude Jul 04 '23 at 06:25
  • 2
    Oh, and what you print is just a presentation of the actual binary values stored in memory. It actually prints the proper binary value, just using the requested presentation. – Some programmer dude Jul 04 '23 at 06:27
  • Thank you, So the issue is inside printf itself? and I can't get the binary without convert the hexdecimal to binary format? – Khaled Jul 04 '23 at 06:28
  • 1
    hexadecimal, octal, and decimal are simply different ways to visualize binary data. You are reading binary data and printing that data as hexadecimal bytes. With a little extra work you could print the individual but values. – Retired Ninja Jul 04 '23 at 06:29
  • Your best bet is to create a function which takes the value as argument together with an array of characters (of at least nine elements). Then it converts the value to binary presentation, in string format using the character array. It can then return the array, and you can use it with the `printf` format `%s` to print the value in binary presentation. – Some programmer dude Jul 04 '23 at 06:29
  • 1
    [Is there a printf converter to print in binary format?](https://stackoverflow.com/questions/111928/is-there-a-printf-converter-to-print-in-binary-format) may help. – Retired Ninja Jul 04 '23 at 06:30
  • @Someprogrammerdude , Thank you very much, your input is appreciated! – Khaled Jul 04 '23 at 06:34
  • As stated in the link provided by @RetiredNinja, in the next version of C (which is called "C23"), you will be able to write `%b` instead of `%x` when using `printf`. Depending on which compiler/standard library you are using and whether it is up to date, if you are lucky, you may already be able to use `%b`. You may want to test whether it works. – Andreas Wenzel Jul 04 '23 at 06:51
  • 1
    @Someprogrammerdude: where do you see any evidence OP knows `%x` is for hexadecimal? The post says they got the code from a forum. The entire text suggests they are largely oblivious to how it works. – Eric Postpischil Jul 04 '23 at 07:35
  • Does this answer your question? [How can I print the binary code of a file in c?](https://stackoverflow.com/questions/68355104/how-can-i-print-the-binary-code-of-a-file-in-c) – nielsen Jul 04 '23 at 08:03
  • Initializing `readsz = sizeof buf` is inconsistent: since `readsz` is used as the number of elements in the array `buf`, you should write `readsz = sizeof buf / sizeof *buf`. Also note that the code could miss the trailing bytes from the file if you used an array size greater than `1`. – chqrlie Jul 04 '23 at 11:06

1 Answers1

1

I think these answers will help you well:

(How can I view the C code after compilation in binary code?)

The code you provided reads a binary file and prints the output in hexadecimal format, not in binary format. The %02x format specifier used in the printf statement formats the output as a two-digit hexadecimal number.

#include <stdio.h>

int main(int argc, char **argv) {
    unsigned char buf[1] = {0};
    size_t bytes = 0, i, readsz = sizeof(buf);

    FILE *fp = fopen("system.bin", "rb");
    if (fp == NULL) {
        printf("Failed to open the file.\n");
        return 1;
    }

    while ((bytes = fread(buf, sizeof(*buf), readsz, fp)) == readsz) {
        for (i = 0; i < readsz; i++) {
            for (int j = 7; j >= 0; j--) {
                if (buf[i] & (1 << j))
                    putchar('1');
                else
                    putchar('0');
            }
            putchar(' ');
        }
    }

    fclose(fp);
    return 0;
}

  • Nice answer! But why did you declare `i` outside the loop? Why not simply declare it inside the loop, as you did with `j`? I see no reason to declare it outside the loop. – Andreas Wenzel Jul 04 '23 at 11:03
  • Initializing `readsz = sizeof(buf)` is inconsistent: since `readsz` is used as the number of elements in the array `buf`, you should write `readsz = sizeof(buf) / sizeof(*buf)`. Also note that the code could miss the trailing bytes from the file if you used an array size greater than `1`. – chqrlie Jul 04 '23 at 11:05