How to convert a binary file to another binary representation, such as an image

I want to take a binary file (exe, msi, dll, whatever) and be able to actually "see" the binary code or any other base that I would like (hexadecimal independently). The easiest way is to simply output the code in txt so that I can learn it.

What is the best and easiest way to do this? I am mainly looking to convert binary to picture for my project.

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

What are your methods for doing this, I have listed C, C ++, and C # because they seem to be the fastest programming languages, and I thought it might take some time. I'm probably more interested in the answer to C, but basically I'm looking for some kind of logic for this.

+5
source share
6 answers

Here you can pack bytes into an image ... the fun part is if you write the original file length and use the lossless image format, you can safely extract binary data later.

Packed like ARGB ...

var exefile = Directory.GetFiles(".", "*.exe").First();
var fi = new FileInfo(exefile);

var dimension = (int)Math.Sqrt((fi.Length + 1d) / 4);

using (var bitmap = new Bitmap(dimension, dimension + 2))
{
    //store the file length in the first pixel.
    bitmap.SetPixel(0, 0, Color.FromArgb((int)fi.Length));

    var buffer = new byte[fi.Length + 4 - fi.Length % 4];
    Array.Copy(File.ReadAllBytes(exefile), buffer, fi.Length);

    int x = 1, y = 0;
    for (var offset = 0; offset < buffer.Length; offset += 4)
    {
        var colorValue = BitConverter.ToInt32(buffer, offset);
        bitmap.SetPixel(x, y, Color.FromArgb(colorValue));

        x++;
        if (x >= dimension)
        {
            x = 0;
            y++;
        }
    }

    bitmap.Save(Path.ChangeExtension(exefile, ".png"), ImageFormat.Png);
}

Packed as a black and white binary ...

var width = (int)Math.Sqrt(fi.Length * 8);
width = width + 8 - (width % 8);
var length = (int)(fi.Length * 8 / width);

Func<byte, int, Color> getcolor =
        (b, m) => (b & m) == m ? Color.Black : Color.White;

using (var bitmap = new Bitmap(width, length + 1))
{
    var buffer = File.ReadAllBytes(exefile);

    int x = 0, y = 0;
    foreach (var @byte in buffer)
    {
        bitmap.SetPixel(x + 0, y, getcolor(@byte, 0x80));
        bitmap.SetPixel(x + 1, y, getcolor(@byte, 0x40));
        bitmap.SetPixel(x + 2, y, getcolor(@byte, 0x20));
        bitmap.SetPixel(x + 3, y, getcolor(@byte, 0x10));

        bitmap.SetPixel(x + 4, y, getcolor(@byte, 0x8));
        bitmap.SetPixel(x + 5, y, getcolor(@byte, 0x4));
        bitmap.SetPixel(x + 6, y, getcolor(@byte, 0x2));
        bitmap.SetPixel(x + 7, y, getcolor(@byte, 0x1));

        x += 8;
        if (x >= width)
        {
            x = 0;
            y++;
        }
    }

    bitmap.Save(Path.ChangeExtension(exefile, ".tif"), ImageFormat.Tiff);
}

... and yes, it sounds like a noise

+13
source

, , , hex editor,

+7

# .NET, ReadAllBytes.

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

.

HexaDecimal, . .

+2

! !

+2

, , , . , (.. 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");

}

StringBuilder, ( ) :

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 
+1
source

Here is the code for printing bytes at 1 and 0 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 own line.
    //printf("\n");
}

int main (int argc, const char * argv[])
{
    int c;

    while ((c = getchar()) != EOF)
    {
        putbits(c);
    }

    return 0;
}

You can create and run it on the command line as follows:

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

Then it will output it 1 and 0 to the printer.txt file.

0
source

All Articles