Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleView the hexadecimal content of a file
Keywordshex, hexadecimal, file, hexview
CategoriesFiles and Directories
 
Use Get to read the file into a byte array quickly. Then format the bytes and display them.
 
' Load and display the file.
Private Sub LoadFile(fname As String, ftitle As String)
Dim fnum As Integer
Dim num_bytes As Integer
Dim bytes() As Byte
Dim txt As String
Dim line1 As String
Dim line2 As String
Dim unk As String
Dim ch As Integer
Dim i As Integer
Dim j As Integer

    ' Prepare the display.
    rchDisplay.Text = ""
    FileName = fname
    FileTitle = ftitle
    Caption = "HexDump [" & ftitle & "]"
    DoEvents

    ' Read the file.
    fnum = FreeFile
    Open fname For Binary As #fnum
    num_bytes = LOF(fnum)
    ReDim bytes(1 To num_bytes)
    Get #fnum, , bytes
    Close fnum

    ' Display the data.
    unk = Chr$(191)
    i = 1
    Do While i <= num_bytes
        line1 = Format$(i, "@@@@@@: ")
        line2 = "    "
        For j = 0 To 7
            If i + j > num_bytes Then
                line1 = line1 & "   "
            Else
                ch = bytes(i + j)
                line1 = line1 & Format$(Hex$(ch), "@@ ")
                If ch >= 32 Then
                    line2 = line2 & Chr$(ch)
                Else
                    line2 = line2 & unk
                End If
            End If
        Next j
        txt = txt & line1 & line2 & vbCrLf
        i = i + 8
    Loop

    rchDisplay.Text = txt
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated