zell
zell

Reputation: 10204

How to get the 0s and 1s from a piece of binary code?

I would like to see 0s and 1s in the binary code.

Say I compile a C code to a.out, cat a.out does not show the raw date but seems to display it as if it were ASCII code. What is the right way to see 0s and 1s?

Upvotes: 0

Views: 48

Answers (1)

Refugnic Eternium
Refugnic Eternium

Reputation: 4291

That depends on the tool you want to use. If you are comfortable with writing your own (for example in C) it would go something like this:

if (FILE *fp = fopen("fileToOpen", "rb"))
{
    uint8_t buffer[4096];
    int32_t readBytes;
    while (0 < (readBytes = fread(buffer, 1, sizeof(buffer), fp))
    {
        for (int32_t i = 0; i < readBytes; ++i)
        {
            for (int32_t j = 0; j < 8; ++j)
                printf("%d", (buffer[i]&(1 << j)) ? 1 : 0;
            printf(" ");
        }
    }
    fclose(fp);
}

Enter formatting as needed.

Upvotes: 1

Related Questions