Hex Editing within Visual Studio 2005

I have been writing Hex quite a bit in the context of constructing unit tests for Lysine. I first was simply creating static byte arrays in the unit tests themselves, however, it started becoming a bit unwieldy as well as providing less than favorable reuse of that test data.

Byte[] testData = new Byte[] { 0xF1, 0x05, 0x5C, 0xF0, 0xF2, 0xF3, 0x4D }

Sure I could have marked it publicly static in a common helper class, but I still did not like writing lines and lines of EBCDIC data in hex byte arrays. I was spending too much time troubleshooting where I didn't type a comma or some other mistake that a typical hex editor would help me avoid.

This led me to the idea of storing this hex data in binary files that I could just load up into memory at test execution time. This later led to me marking these files to be Embedded into the unit test assembly and using the GetManifestResourceStream(string name) method of my currently executing Assembly to pull the contents of the embedded file into a byte array for me to test against. These eventually went into a Helper class that consist purely of a few static methods like such:

public static byte[] GetBytes(string resourceName) { System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly(); System.IO.Stream unmanagedStream = assembly.GetManifestResourceStream(resourceName); byte[] buffer = new byte[unmanagedStream.Length]; unmanagedStream.Read(buffer, 0, buffer.Length); unmanagedStream.Close(); return buffer; }

What these embedded files give me now is a cool feature of VS2005, that I didn't knew existed -- an embedded lightweight hex editor. By double-clicking one of the embedded binary files (that I have named .bin, I get a tabbed document providing a simple hex editor to modify the data:

Tags: hex edit, visual studio, embedded data, unit testing