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

前端 未结 6 797
花落未央
花落未央 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 23:04

    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 
    

提交回复
热议问题