Highlights
All Snippets
Top 100 Snippets
Librarians
gbCodeLib

By Language
VB6
JavaScript
Perl
HTML
SQL
Java
DOS

GBIC >> Source Code >> Visual Basic >> Snippet

View in hex format


Sub HexView(FileName As String )
'put contents of file into textbox1
Dim temp As String , temp2 As String
Dim i As Long , j As Long
Dim leader As String
Dim HexText As String
Dim AsciiText As String
Dim Char As String

'read the file into a string
Open filename For Input As #1
temp2 = Input (Lof(1),#1)
Close #1

'create hex string
If Len(temp2) > 0 Then
    For i = 1 To Len(temp2) Step 8
       leader = String $((6 - Len( Hex (i - 1))), "0" ) & Hex (i - 1) & " "
       HexText = ""
        For j = 0 To 7
            If i + j > Len(temp2) Then
               HexText = HexText & Space$((8 - j) * 3)
                Exit For
            End If
            Char = Format $( Hex ( Asc ( Mid $(temp2, i + j, 1))), "00" )
            If Len( Char ) = 1 Then Char = "0" & Char
           HexText = HexText & Char & " "
        Next j
       AsciiText = Mid $(temp2, i, 8)
       AsciiText = Replace(AsciiText, Chr (9), "." )
       AsciiText = Replace(AsciiText, Chr (10), "." )
       AsciiText = Replace(AsciiText, Chr (13), "." )
       temp = temp & leader & HexText & " " & AsciiText & vbCrLf
    Next i
Else
   temp = vbCrLf
End If

temp = Mid $(temp, 1, Len(temp) - 2)
text1.text = temp

End Sub