tags:

views:

286

answers:

6

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 way would be just to output the code into a txt so I can examine it.

Whats the best and easiest way to do this? Basically I am looking to convert the binary code into a picture for a project of mine.

Similarly, it would be nice if I could take some binary code, and then convert it into a binary file.

What are your methods for doing this, I listed C, C++, and C# because these seem to be the fastest programming languages and I figured this may take some time. I guess I am more interested in an answer in C, but I am mostly looking for some logic behind this.

+2  A: 

You can read all bytes of a file in C# or any .NET language by calling the static method ReadAllBytes.

byte[] allBytes = File.ReadAllBytes("YourPath");

It will put all the bytes into an array.

If you want to covert it to HexaDecimal see here.

David Basarab
This is what I want. Thanks.Any method to taking bytes to a file.I do not use c# or any .NET languages (yet). Can I assume its File.WriteAllBytes :)
BHare
+7  A: 

I'm not quite sure what you are trying to do, but it sounds a bit like you're looking for a hex editor.

Thomas
Yes but can switch from hex to binary or whatever. I didnt think they had "binary modes" in hex editors but I guess I should google it. I still need to extract this code into something I can work with in an automated fashion (a txt file or something)
BHare
You can't switch from hex to binary in your head? That's the whole point of hexadecimal. This is literally a hex editor's entire purpose in life, so that's almost certainly what you want.
mquander
OK. I am wanting to make a black pixel for 1 and a white pixel for 0. This is why I need to "see" the binary. I guess I could copy and paste the hexadecimal from the hex editor and then have a program convert it to binary...but...The whole point to convert it to binary in the first place?
BHare
Oh. I think people were getting confused, then. You said you want to "see" it in binary, but you don't want to see it at all; you want to process it with a program and create a bitmap out of it. I agree then with the above answer -- you ought to use File.ReadAllBytes and then process the bytes to make your image. A hex editor is not helpful for you, since you're not interested in examining or editing it manually.
mquander
@Brian, okay... that is more nuts than my example would host. using a pixel per bit will make a huge image.
Matthew Whited
+2  A: 

Already done for you! Very convieniently a binary file is already stored in binary!

STW
I know. How can I see this binary code and edit it easily. I guess like a hex editor, but a binary editor lol.
BHare
@Brian: A hex editor *is* a binary editor: it shows you a visual representation of the binary code, which is commonly done in hexadecimal, byte for byte. When you edit inside a hex editor, it will be converted back to the underlying binary representation of that byte.
Abel
I dont want to see it in hex. I want to see it in binary. I won't be editing it manually, but rather applying a technique.
BHare
I think he just wants to see 1's and 0's.
STW
@Brian: you can't *see* binary. Just like you can't *see* a BLOB in a database. The only way to see it, is to use some representation. An image will tell you little, hex, octal, binary (zero/one) are string representations. But your idea is fun, useless, but fun: making an image of it is yet another non-binary representation (in the picture viewer) in another binary format (the image file) of a binary file (the original EXE). Confusing?
Abel
+10  A: 
Matthew Whited
Yes. This is basically what I want to do.
BHare
+1 for giving a quick and useful example of a rather funny exercise. I didn't come any further than creating a hex-viewer in that timeframe :)
Abel
Matthew Whited
+1 for being able to understand what the question was
Victor Hurdugaci
@Victor, lol... thanks :o)
Matthew Whited
Matthew Whited
I ran into a problem with var colorValue-it states: Destination array is not long enough to copy all the items in the collection. Check array index and length.Weird because it made a png of a 8MB file, but its choking on a 400KB one.I just downloaded Visual C# 2010 express...so not really sure (yet) how to move on. Any help is appreciated :)..Edit: just noticed you did one for black and white (you are awesome)...trying it now...
BHare
@Brian, yeah you may need to add a bounds check to the part that uses `BitConverter.ToInt32(buffer, offset);`. That will try to grab the next 4 bytes out of the byte[] and if it's not something that is divisible by 4 that could be an issue.
Matthew Whited
@Brian, see the change I just added.
Matthew Whited
+1  A: 

Just triggered by your question, I come in a bit late in the discussion, but wondered how easy it could be done. Here's a minimal implementation that gives you the binary output of the currently executing assembly (i.e. your current running EXE):

byte[] bytes = File.ReadAllBytes(Assembly.GetExecutingAssembly().Location);

// this can get large, we know how large, so allocate early and try to be correct
// note: a newline is two bytes
StringBuilder sb = new StringBuilder(bytes.Length * 3 + (bytes.Length / 16) * 4);

for (int i = 0; i < bytes.Length; i++)
{
    sb.AppendFormat("{0:X2} ", bytes[i]);
    if (i % 8 == 0 && i % 16 != 0)
        sb.Append("  ");
    if (i % 16 == 0)
        sb.Append("\n");

}

If you output the StringBuilder contents, you see the following (which is my test executable) for the first some bytes:

5A 90 00 03 00 00 00 04   00 00 00 FF FF 00 00 B8 
00 00 00 00 00 00 00 40   00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00   00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00   00 00 00 80 00 00 00 0E 
1F BA 0E 00 B4 09 CD 21   B8 01 4C CD 21 54 68 69 
73 20 70 72 6F 67 72 61   6D 20 63 61 6E 6E 6F 74 
20 62 65 20 72 75 6E 20   69 6E 20 44 4F 53 20 6D 
6F 64 65 2E 0D 0D 0A 24   00 00 00 00 00 00 00 50 
Abel
A: 

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

#include <stdio.h>

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.

Douglas