Viewing Binary Data Using ByteViewer

Did you know that the .NET framework has a built-in control/class for displaying the hexadecimal byte dump output of binary data? It's one of those rare gems that pretty much stays hidden away unless you actually need to go looking for it.

For those who are wondering what I'm talking about, I'm describing the type of view that loads in a file and displays it in this fashion:

Binary1
Figure 1: Example binary data byte dump

Although in many cases, you'll very often just handle easy-to-display formats (such as JSON, XML, and CSV), you do occasionally have to be able to display raw binary.

In the example in Figure 1, you're looking at a compiled EXE file, but you could just as easily be looking at a JPG Image, a wave audio file, or just some bizarre, strange custom format.

There are a large number of custom components out there to do this task, and, with a little bit of forward thinking and some creative use of "String.Format" and a Listbox control, it's not too difficult to roll your own and format the display in any manner you want.

The .NET runtime itself, however, actually has (and has had for quite some time now) a control already built in, that will perform this exact task, simply, quickly, and elegantly for you.

Let's have a look at how to use it.

Create yourself a simple Windows Forms application. (As far as I know, you can't use this control in WPF. If anyone knows otherwise, however, please let me know in the comments so I can add it.)

Once you have a project open, add a File Open Dialog to your form, so that it looks something like this:

Binary2
Figure 2: Visual Studio form designer with an 'openFile' dialog added

You'll notice from Figure 2 that I've also added a menu strip, a panel, and a couple of Menu items so I can load in a binary file and exit the application.

Press F7 to open the code behind for your form, and make sure the code looks as follows:

using System;
using System.Windows.Forms;

namespace ByteViewer
{
   public partial class Form1 : Form
   {
      private System.ComponentModel.Design.ByteViewer
         _myByteViewer;

      public Form1()
      {
         InitializeComponent();
         _myByteViewer = new System.ComponentModel.Design.ByteViewer
            {Dock = DockStyle.Fill};
         viewerPanel.Controls.Add(_myByteViewer);
      }

      private void OpenToolStripMenuItemClick(object sender,
         EventArgs e)
      {
         if(openFile.ShowDialog() == DialogResult.OK)
         {
            _myByteViewer.SetFile(openFile.FileName);
         }
      }

      private void ExitToolStripMenuItemClick(object sender,
         EventArgs e)
      {
         Application.Exit();
      }

   }
}

Make sure that you hook the two menu event handlers up to your menu by clicking them, and also note that the blue panel used in the form design has been given the name 'viewerPanel'.

If you have everything set up as I do, you should be able to run the application. Then, choose 'FIle->Open' from the menu, select a file, and see the file's binary contents in your form:

Binary3

Figure 3: Our Byte Viewer form showing an open binary file (in this case, a Zip)

As you can see, it's fast and easy to use, ideal if you just need a quick and dirty hex data viewer.

Got a sticky .NET problem you need describing? Shout me up in 'Lidnug' on Linked-in, or come find me on Twitter as @Shawty_ds and let me know. I'll most likely write a post about it.



Related Articles

Comments

  • Binary editor?!

    Posted by Zibri on 11/06/2016 12:28pm

    There is also a Design.BinaryEditor Do you know how to use it?

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date