How can I convert a binary file to another binary representation, like an image

前端 未结 6 806
花落未央
花落未央 2020-12-20 22:10

I want to take a binary file (exe, msi, dll, whatever) and be able to actually \"see\" the binary code or whatever base I\'d like (hexadecimal whatever). Figured the easiest

6条回答
  •  南笙
    南笙 (楼主)
    2020-12-20 22:50

    Here's the code to print out the bytes at 1's and 0's in C:

    #include 
    
    void putbits(unsigned char byte)
    {
        unsigned char mask = 0x01;
    
        for (int bit = 7; bit >= 0; bit--)
        {
            if ((mask << bit) & byte)
            {
                printf("1");
            }
            else
            {
                printf("0");
            }
        }
    
        // Uncomment the following line to get each byte on it's own line.
        //printf("\n");
    }
    
    int main (int argc, const char * argv[])
    {
        int c;
    
        while ((c = getchar()) != EOF)
        {
            putbits(c);
        }
    
        return 0;
    }
    

    You can build and run it on the command line like this:

    gcc main.c --std=c99 -o printer
    ./printer < printer > printer.txt
    

    It will then output it's 1's and 0's to printer.txt.

提交回复
热议问题