Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
667 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's a way to pack the bytes into an image... the fun part is if you record the original file length and use a lossless image format you could safely extract the binary data later.

Packed as 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 Black & 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 yeah, it looks like noise


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...