Binary File Display

Binary File Display

An example that reads the contents of a file and displays the contents in a formatted text box as hex and text.

Key Points:
  – GUI controls created from code (No form design required)
  – File Open dialog
  – Binary file read
  – Formatted text display

This example presents the user with a file open dialog box that is used to indicate the file to be displayed.  The contents of the file are then loaded into a text box that shows each byte in hex format (laid out as 2 characters) and, if possible, the printable text represented by the character.  The data is laid out in numbered rows of 16 bytes per row.

The example demonstrates how .Net enables apparently complex applications to be developed with just a few lines of code.

The form uses only two GUI controls – a Open File menu item and the text box.  It is therefore suitable as an example of how all the GUI forms on a control can be created by code – no design is required.  The only slightly complex part is the calculation of location and size for the text box.

Imports System.IO
Imports System.Text
Public Class Form1
  Dim mnuMenuStrip As New MenuStrip
  Dim WithEvents mnuFileOpen As New ToolStripButton
  Dim tbxContents As New TextBox
  Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  Me.Text = "File Contents Display"
  mnuFileOpen.Text = "File Open"
  mnuMenuStrip.Items.Add(mnuFileOpen)
  mnuMenuStrip.Parent = Me
  tbxContents.Multiline = True
  tbxContents.ScrollBars = ScrollBars.Both
  tbxContents.ReadOnly = True
  tbxContents.Font = New Font("Courier new", 8)
  tbxContents.Location = New Point(0, mnuMenuStrip.Location.Y + mnuMenuStrip.Height)
  tbxContents.Size = New Size(Me.ClientSize.Width, Me.ClientSize.Height - tbxContents.Height)
  tbxContents.Anchor = AnchorStyles.Bottom + AnchorStyles.Left + AnchorStyles.Right + AnchorStyles.Top
  tbxContents.Parent = Me
End Sub
Private Sub FileBtn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles mnuFileOpen.Click
  Dim dlgFileOpen As New OpenFileDialog
  If dlgFileOpen.ShowDialog = DialogResult.OK Then
    tbxContents.Visible = False
    tbxContents.Clear()
    Dim FS As New FileStream(dlgFileOpen.FileName, IO.FileMode.Open)
    If FS.Length > Int32.MaxValue Then
      MsgBox("File is too large to display.")
    Exit Sub
    End If
    Dim B(15) As Byte  
    Dim I As Integer
    tbxContents.Text = "Address 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F Text " & Environment.NewLine
    tbxContents.Text &= "------------------------------------------------------- ----------------" & Environment.NewLine
    Do
      Dim strHexDisplay As New StringBuilder()
      Dim strChrDisplay As New StringBuilder()
      For I = 0 To FS.Read(B, 0, 16) - 1
        strHexDisplay.Append(Hex(B(I)).PadLeft(2, "0"c) & " ")
        strChrDisplay.Append(IIf(B(I) > 31 And B(I) < 128, Chr(B(I)), "."))
      Next
      tbxContents.AppendText(Hex(FS.Position).PadLeft(7, "0"c) & " " & _
        strHexDisplay.ToString & _
        strChrDisplay.ToString & Environment.NewLine)
    Loop Until I < 16
    FS.Close()
    End If
  tbxContents.Visible = True
  End Sub
End Class

 

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s