主题:寻求VB代码
51pywg
[专家分:1540] 发布于 2009-05-21 22:42:00
如何用VB以16进制形式读取出一个文件的数据,像bmp文件
回复列表 (共2个回复)
沙发
一江秋水 [专家分:9680] 发布于 2009-05-22 08:35:00
下面以读取一个文本文件为例:
Private Sub 读文件_Click(Index As Integer)
On Error GoTo 100
Dim DAT() As Byte, Nam As String, St As String, fLen As Long
CD.Filter = "所有文件(*.*)|*.*"
CD.ShowOpen
Nam = CD.FileName
If Nam = "" Or Dir$(Nam) = "" Then Exit Sub
Select Case Index
Case 0 '二进制读入,一次性先读入到字节数组,经转换后再赋给Text1
fLen = FileLen(Nam) - 1
ReDim DAT(fLen) As Byte
Open Nam For Binary As #1
Get #1, , DAT
Close
Text1 = StrConv(DAT, vbUnicode)
Case 1 '二进制读入,一次性先读入到字串变量,再赋给Text1
St = Space(FileLen(Nam) - 1) '先将字串变量填入文件长度的空格,这一句是必须的
Open Nam For Binary As #1
Get #1, , St
Close
Text1 = St
Case 2 '二进制读入,读入后以16进制数据形式显示
Dim H() As String
fLen = FileLen(Nam) - 1
ReDim DAT(fLen) As Byte, H(fLen) As String
Open Nam For Binary As #1
Get #1, , DAT
Close
For i = 0 To fLen
H(i) = Hex(DAT(i)): If Len(H(i)) = 1 Then H(i) = "0" & H(i)
St = St & H(i) & " "
Next
Text1 = St
End Select
Me.Caption = Nam
Exit Sub
100
Close
End Sub
板凳
guoyong_cy [专家分:3050] 发布于 2009-05-22 12:13:00
[quote]
For i = 0 To fLen
H(i) = Hex(DAT(i)): If Len(H(i)) = 1 Then H(i) = "0" & H(i)
St = St & H(i) & " "
Next
Text1 = St
[/quote]
这样子转换,完都完了,就是32k的数据也要花10多秒,要是读个bmp(1M),不卡死才怪!
这种反复字符串操作是很慢的,最好用byte数组操作
我来回复