Imports System
Imports System.IO
Imports System.Text
Public Class DumpFileSample
Private Shared ReadOnly CHUNK_SIZE As Integer = 1024
Public Shared Sub Main(args() As String)
Try
Dim fs As FileStream
If args.Length > 0 Then
fs = New FileStream(args(0), FileMode.Open, FileAccess.Read)
Else
Throw New ArgumentException()
End If
' use one byte per character encoding for reading dump data
Dim br As New BinaryReader(fs, New ASCIIEncoding())
Dim chunk(CHUNK_SIZE) As Byte
Dim chunksize as Integer
' read file chunks until end of file
Do
chunksize = br.Read(chunk, 0, CHUNK_SIZE)
' chunksize will be 0 if end of file is reached.
If chunksize > 0
DumpBytes(chunk, chunksize)
End If
Loop While chunksize > 0
fs.Close()
Catch
Console.WriteLine("Cannot find or read file to dump.")
End Try
End Sub
Public Shared Sub DumpBytes(bdata() As Byte, len As Integer)
Dim i As Integer
Dim j As Integer = 0
Dim dchar As Char
' 3 * 16 chars for hex display, 16 chars for text and 8 chars
' for the 'gutter' int the middle.
Dim dumptext As New StringBuilder(" ", 16 * 4 + 8)
For i = 0 to len -1
dumptext.Insert(j * 3, String.Format("{0:X2} ", CType(bdata(i), Integer)))
dchar = Convert.ToChar(bdata(i))
' replace 'non-printable' chars with a '.'.
If Char.IsWhiteSpace(dchar) Or Char.IsControl(dchar) Then
dchar = "."
End If
dumptext.Append(dchar)
j += 1
If j = 16 Then
Console.WriteLine(dumptext)
dumptext.Length = 0
dumptext.Append(" ")
j = 0
End If
Next i
' display the remaining line
If j > 0 Then
' add blank hex spots to align the 'gutter'.
For i = j To 15
dumptext.Insert(j * 3, " ")
Next i
Console.WriteLine(dumptext)
End If
End Sub
End Class