The code below writes a binary file, then reads it into a byte array and outputs the hexadecimal values.
using (var writer = new BinaryWriter(File.Create("foo.bin")))
{
writer.Write(1.250F);
writer.Write(@"c:\Temp");
writer.Write(10);
writer.Write(true);
}
var bytes = File.ReadAllBytes("foo.bin");
var text = BitConverter.ToString(bytes);
Console.WriteLine(text);
Here's the output:
00-00-A0-3F-07-63-3A-5C-54-65-6D-70-0A-00-00-00-01
Decoding, we have:
0000A03f - 4 bytes = 1.250F
Next are 8 bytes that correspond to the string "c:\Temp". The first byte, 07
, is the length. BinaryWriter
uses a 7-bit encoding scheme for the length, so strings shorter than 128 bytes only require a single length byte. The 7 characters that make up the string follow the length byte.
Following that you have the four bytes 0A000000
, which is the integer 10.
Finally, you have the value 1, which is the boolean True.
You should use BitConverter.ToString(bytes)
to output the data from your file. Then you can examine the bytes directly.
If you have some idea of what's in the file, you can usually puzzle it out. Strings are easy to pick out, and you can often use them as anchors to figure out what's on either side of them. This is especially true if you have knowledge of what's in the file and you're just trying to figure out the order in which things were written.
You don't really need a decompiler to figure out the order he wrote things in. You can use ildasm.exe to examine the generated MSIL. The function calls to BinaryWriter.Write
will be easy to spot, and the disassembled code will tell you whether it's writing a string, Boolean, integer, etc. For a one-time thing, that's the way I'd go. If I had a larger job where I needed to understand a lot of code, I'd go with a better decompiler.