在VB中實現任意檔案的16進位制方式察看 (轉)

gugu99發表於2008-05-25
在VB中實現任意檔案的16進位制方式察看 (轉)[@more@]

  這是我利用操作方法編寫的檔案16進位制察看。本來是可以利用UltraEdit來幹這件事的。我編寫的目的就是想看一下自己能不能編出來。於是我就用VB編了一下,先前我只是把直接放到網上,沒想到大家很支援我,給了我很大的鼓舞,在這裡我十分感謝eastunfail的幫助,使我的程式碼更加完善。

原始碼如下:

Dim col As ColumnHeader
Dim litem As ListItem
Dim sb As ListSubItem
Dim filename As String
Dim index As Long

Private Sub Command1_Click()
Dim b As Byte
'Dim ff() As Byte
Dim fl As Long
Dim sect As Long
Dim ss As Long

ListView1.ListItems.Clear

dlg.ShowOpen
filename = dlg.filename
fl = FileLen(filename)
sect = fl / 16
ss = fl Mod 16


Open filename For Binary As #1
  If sect = 0 Then
  Set litem = ListView1.ListItems.Add()
  For index = 1 To ss
  Get #1, index, b
  Set sb = litem.ListSubItems.Add(index, , DecToHex(b))
  Next index
  End If
 
  If sect <> 0 Then
  For i = 1 To sect
  Set litem = ListView1.ListItems.Add()
  For index = 1 To 16
  Get #1, 16 * (i - 1) + index, b
  Set sb = litem.ListSubItems.Add(index, , DecToHex(b))
  Next index
  Next i
  Set litem = ListView1.ListItems.Add()
  For index = 1 To ss
  Get #1, index, b
  Set sb = litem.ListSubItems.Add(index, , DecToHex(b))
  Next index
  End If
Close #1

End Sub


Private Sub Command2_Click()
MsgBox "Welcome To Three Leaf WorkRoom!!", , "About"
End Sub


Private Sub Form_Load()
Dim i As Integer

ListView1.ColumnHeaders.Add , , "Address", ListView1.Width / 14

For i = 1 To 16
Set col = ListView1.ColumnHeaders.Add()
  col.Text = "+" & DecToHex(i - 1)
  col.Width = ListView1.Width / 25
Next

ListView1.ColumnHeaders.Add , , "ASCII", ListView1.Width / 4

End Sub

Private Function DecToHex(dd As Byte) As String
DecToHex=IIf(Dec>&HF,Hex(dd),"0" & Hex(dd))  '這行程式碼是eastunfail提供的。
End Function


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10748419/viewspace-1004560/,如需轉載,請註明出處,否則將追究法律責任。

相關文章