我怎样才能二进制文件转换为另一种二进制重新presentation,像一个形象 [英] How can I convert a binary file to another binary representation, like an image

查看:139
本文介绍了我怎样才能二进制文件转换为另一种二进制重新presentation,像一个形象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想带一个二进制文件(EXE,MSI,DLL,等等),并能够真正看到二进制code或任何基础,我想(十六进制什么)。想通最简单的方法是只输出code成TXT,所以我可以检查它。

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.

最新最好的和最简单的方式做到这一点?基本上,我期待二进制code转换成图片我的一个项目。

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.

同样,这将是很好,如果我能采取一些二进制code,然后将其转换成二进制文件。

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

什么是你这样做的方法,我列出了C,C ++和C#,因为这些似乎是最快的编程语言和我想这可能需要一些时间。我想我更感兴趣的是C中的答案,但我主要是寻找这背后的一些逻辑。

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.

推荐答案

下面是收拾字节到图像的方式......最有趣的部分是,如果你记录原始文件长度,并使用无损图像格式,你可以安全地后来提取二进制数据。

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.

盒装作为ARGB ...

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

这篇关于我怎样才能二进制文件转换为另一种二进制重新presentation,像一个形象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆