Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / DevOps / testing

Include a binary file in your source code as a byte array

4.57/5 (4 votes)
30 Jun 2011CPOL 33.1K  
A small method that reads a binary file from disk and creates valid byte[] declaration with the content of that file which you can then copy & paste into your application. This can be useful in unit tests if you need to feed binary data to the tested method.
I recently developed a C# based binary message parser for a proprietary communication protocol. I had a bunch of sample messages in files on my disk which were raw captures from Wireshark[^] and I used them extensively during the development of the protocol parser.

I also wanted to use the same messages during unit testing which posed a small dilemma: I wanted the tests to be self contained and not rely on any external resources - not even local files (this is generally a good practice when writing unit tests by the way). The solution is to convert the files into a byte[] declaration that can be included in the unit test source code file.

This particular example will read all files with the *.bin extension in the specified folder and output to a text file called byte-arrays.txt in that folder with the array declarations.

C#
private static void CreateByteArrays(DirectoryInfo directory)
{
    var outputFile = Path.Combine(directory.FullName, "byte-arrays.txt");
    using (var writer = File.CreateText(outputFile))
    {
        int fileNumber = 1;
        foreach (var file in directory.GetFiles("*.bin"))
        {
            writer.WriteLine("private static byte[] msg{0} = ", fileNumber++);
            writer.Write("    {");
            using (var reader = new BinaryReader(file.OpenRead()))
            {
                for (int i = 0; i < file.Length; i++)
                {
                    // Wrap to new line after 15 bytes 
                    if (i % 15 == 0)
                    {
                        writer.WriteLine();
                        writer.Write("           ");
                    }
                    writer.Write("0x{0:X2}, ", reader.ReadByte());
                }
            }
            writer.WriteLine();
            writer.WriteLine("    };");
        }
    }
}

Use it like so:
C#
CreateByteArrays(new DirectoryInfo(@"C:\my\folder\containing\dumps"));


The result is a text file which looks something like this:
C#
private static byte[] msg1 =
    {
           0x1E, 0x00, 0x00, 0x00, 0x0E, 0x04, 0x47, 0x00, 0x00, 0x00, 0x00, 0x00, 
           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xFF, 0x18, 0x0B, 0x0E, 0xFF, 
           0x12, 0x03, 0x00, 0x00, 0x0E, 0x6D, 0x15, 0x34, 0x15, 0x20, 0x12, 0x10, 
           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xEC, 0x16,
    };
private static byte[] msg2 =
    {
           0x1F, 0x00, 0x00, 0x00, 0x04, 0x29, 0x92, 0x11, 0x00, 0x00, 0x04, 0xA9, 
           0x00, 0x00, 0x00, 0xB7, 0x16,
    };
private static byte[] msg3 =
    {
           0xC1, 0x80, 0x40, 0xFD, 0x1B, 0x01, 0x8E, 0x80, 0x40, 0xFD, 0x61, 0x04, 
           0x00, 0x00, 0x81, 0x40, 0xFD, 0x1A, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x00, 
    };
private static byte[] msg4 =
    {
           0x68, 0x4F, 0x4F, 0x68, 0x08, 0x00, 0x72, 0x72, 0x16, 0x41, 0x00, 0x42, 
           0x21, 0x00, 0x00, 0x00, 0xCE, 0x00, 0xED, 0xEB, 0x15, 0x00, 0x00, 0x00, 
           0xCE, 0x40, 0x84, 0x15, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xCE, 0x80, 
           0x15, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x0B, 0x16,
    };

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)