Click here to Skip to main content
Click here to Skip to main content

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

, 30 Jun 2011
Rate this:
Please Sign up or sign in to vote.
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.
 
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:
CreateByteArrays(new DirectoryInfo(@"C:\my\folder\containing\dumps"));
 
The result is a text file which looks something like this:
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)

About the Author

isaks
Software Developer ABB
Sweden Sweden
My name is Isak Savo and I work as a Research Engineer at ABB Corporate Research in Västerås, Sweden. My work is focused around user experience which includes a lot of prototyping of new solutions for user interfaces and user interaction.
 
While I have a background in C programming in a Linux environment, my daily work is mostly spent in Windows using C# and WPF.
Follow on   Twitter

Comments and Discussions

 
QuestionBeware of stack limits in c/c++ Pinmemberheathbar12-Apr-13 14:22 
GeneralReason for my vote of 5 Simple and to the point. Thanks for ... Pinmemberpscholl4-Jul-11 23:24 
GeneralReason for my vote of 3 I use this in my project too Pinmemberzf.liu4-Jul-11 23:10 
GeneralReason for my vote of 5 Very interesting solution, nice to h... PinmemberDrABELL4-Jul-11 9:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 1 Jul 2011
Article Copyright 2011 by isaks
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid