이진 파일을 읽어서 16진수로 보여주는 HexView 소스 with Visual BASIC
Visual BASIC 언어 소스:
REM ============================================================================
REM Filename: TestHexView_03.vb
REM
REM Compile: vbc TestHexView_03.vb
REM Execute: TestHexView_03 [filename]
REM
REM Date: 2013. 8. 6.
REM ============================================================================
Imports System.IO
Public Class HexViewUtil
Shared Sub PrintUsage()
Console.WriteLine("Usage: TestHexView_03 [filename]")
End Sub
Shared Function ToHex(c As Integer)
Dim s As String = ""
Dim x1, x2 As Integer
x1 = (c And &HF0) >> 4
x2 = c And &H0F
If x1 < 10 Then
s = s & Chr(x1 + Asc("0"))
Else
s = s & Chr((x1 - 10) + Asc("A"))
End If
If x2 < 10 Then
s = s + Chr(x2 + Asc("0"))
Else
s = s + Chr((x2 - 10) + Asc("A"))
End If
ToHex = s
End Function
Shared Function ToHex8(n As Integer)
Dim s As String = ""
Dim x1, x2, x3, x4, x5, x6, x7, x8 As Integer
x1 = (n And &HF0000000) >> 28
x2 = (n And &HF000000) >> 24
x3 = (n And &HF00000) >> 20
x4 = (n And &HF0000) >> 16
x5 = (n And &HF000) >> 12
x6 = (n And &HF00) >> 8
x7 = (n And &HF0) >> 4
x8 = n And &H0F
If x1 < 10 Then
s = s & Chr(x1 + Asc("0"))
Else
s = s & Chr((x1 - 10) + Asc("A"))
End If
If x2 < 10 Then
s = s + Chr(x2 + Asc("0"))
Else
s = s + Chr((x2 - 10) + Asc("A"))
End If
If x3 < 10 Then
s = s + Chr(x3 + Asc("0"))
Else
s = s + Chr((x3 - 10) + Asc("A"))
End If
If x4 < 10 Then
s = s + Chr(x4 + Asc("0"))
Else
s = s + Chr((x4 - 10) + Asc("A"))
End If
s = s + " "
If x5 < 10 Then
s = s + Chr(x5 + Asc("0"))
Else
s = s + Chr((x5 - 10) + Asc("A"))
End If
If x6 < 10 Then
s = s + Chr(x6 + Asc("0"))
Else
s = s + Chr((x6 - 10) + Asc("A"))
End If
If x7 < 10 Then
s = s + Chr(x7 + Asc("0"))
Else
s = s + Chr((x7 - 10) + Asc("A"))
End If
If x8 < 10 Then
s = s + Chr(x8 + Asc("0"))
Else
s = s + Chr((x8 - 10) + Asc("A"))
End If
ToHex8 = s
End Function
Shared Sub Main(args() as String)
Dim fname = ""
If args.Length < 1 Then
PrintUsage()
Exit Sub
Else
fname = args(0)
End If
If My.Computer.FileSystem.DirectoryExists(fname) Then
Console.WriteLine("The path ""{0}"" is a directory.", fname)
Exit Sub
End If
If My.Computer.FileSystem.FileExists(fname) = False Then
Console.WriteLine("The file ""{0}"" does not exist.", fname)
Exit Sub
End If
Dim info As New FileInfo(fname)
Dim fsize As Long = info.Length
Console.WriteLine("The size of the file ""{0}"" is {1}.", fname, fsize)
Console.WriteLine()
Dim inputFile = IO.File.Open(fname, IO.FileMode.Open)
Dim bufferSize = 16
Dim bytes = New Byte(bufferSize) {}
Dim n = 0
Dim m = 0
Dim i = 0
Dim j = 0
Dim t = ""
Dim c = 0
While n < fsize
m = inputFile.Read(bytes, 0, bufferSize)
If n Mod 16 = 0 Then
Console.Write("{0}: ", ToHex8(n))
End If
For i = 0 To m - 1
c = bytes(i)
If i = 7 Then
Console.Write("-{0}", ToHex(c))
Else
Console.Write(" {0}", ToHex(c))
End If
If c - Asc(" ") < 0 Or c > &H7F Then
t = t + "."
Else
t = t + Chr(c)
End If
Next i
If m < 16 Then
For j = 1 To 16 - m
Console.Write(" ")
Next j
End If
Console.Write(" |{0}", t)
t = ""
If m < 16 Then
For j = 1 To 16 - m
Console.Write(" ")
Next j
End If
Console.WriteLine("|")
n = n + m
End While
inputFile.Close()
Console.WriteLine()
Console.WriteLine("Read {0} bytes.", n)
End Sub
End Class
실행 예 1> TestHexView_03 temp_1.bin
The size of the file "temp_1.bin" is 12.
0000 0000: 48 65 6C 6C 6F 20 74-68 65 72 65 0A |Hello there. |
Read 12 bytes.
실행 예 2> TestHexView_03 myFile.ser
The size of the file "myFile.ser" is 130.
0000 0000: AC ED 00 05 73 72 00-06 50 65 72 73 6F 6E 07 31 |....sr..Person.1|
0000 0010: 46 DB A5 1D 44 AB 02-00 03 49 00 03 61 67 65 4C |F...D....I..ageL|
0000 0020: 00 09 66 69 72 73 74-4E 61 6D 65 74 00 12 4C 6A |..firstNamet..Lj|
0000 0030: 61 76 61 2F 6C 61 6E-67 2F 53 74 72 69 6E 67 3B |ava/lang/String;|
0000 0040: 4C 00 08 6C 61 73 74-4E 61 6D 65 71 00 7E 00 01 |L..lastNameq.~..|
0000 0050: 78 70 00 00 00 13 74-00 05 4A 61 6D 65 73 74 00 |xp....t..Jamest.|
0000 0060: 04 52 79 61 6E 73 71-00 7E 00 00 00 00 00 1E 74 |.Ryansq.~......t|
0000 0070: 00 07 4F 62 69 2D 77-61 6E 74 00 06 4B 65 6E 6F |..Obi-want..Keno|
0000 0080: 62 69 |bi |
Read 130 bytes.