vb.net 读写 HEX 文件

maurice1978 2010-08-13 11:43:37
怎样读取分析一个HEX 文件??

从网上下了一些VB代码,不能直接在VB2005 中使用(红色字体部分有问题),谁能帮忙改一下,代码:
---------------------------------------
Function Open_Hex_File(HexFileName As String) As Boolean
On Error GoTo Error_Handel
Dim i As Long 'ÁÙʱ±äÁ¿
Dim j As Integer 'ÁÙʱ±äÁ¿
Dim k As Integer 'ÁÙʱ±äÁ¿
Dim x As Integer 'ÁÙʱ±äÁ¿

Dim ttext As String

Dim Hexfile_RowCount As Long 'HexÎļþµÄÐÐÊý
Dim Hexfile_Datalenght_PreRow As Integer 'ÿÐÐÎļþµÄÓÐЧÊý¾ÝÊý
Dim Hexfile_Data() As Byte 'ÿÐÐÎļþµÄÄÚÈÝ
Dim HexFile_CheckSum As Integer 'CheckSum
Dim HexFile_StartAddress As Long 'ROMλÖÃ
Dim Temp_Add_Max As Long 'µØÖ·×î¸ßλ
Dim Max_Byte_PreRow As Integer 'ÿÐеÄbyteÊý
Dim Temp_Chenksum_PreRow As Long '¼ÆËã³öÀ´µÄchecksum
Dim tFileNumber As Integer 'ÎļþºÅ

'³õʼ»¯±äÁ¿
HexFile_Lenght = 0
Temp_Add_Max = 0
Temp_Chenksum_PreRow = 0

'Çå¿ÕhexÊý×é
For i = 0 To BUFFERLEN
Hex_File_Data_Buffer(i) = &HFF '¶ÔËùÓлº³åÊý¾ÝÌîFF,Ò²¿ÉÌî0,¸ù¾Ýʵ¼ÊÇé¿ö¶ø¶¨
Next i

'»ñÈ¡Îļþ×ÜÐÐÊý
ttext = TextGetByFile(HexFileName) '½«Õû¸öÎı¾¶Á³ö¸³Öµ¸øttext
Hexfile_RowCount = UBound(Split(ttext, vbCrLf)) 'È¡³öÐÐÊý

'´ò¿ªÎļþ
tFileNumber = FreeFile
Open HexFileName For Input As #tFileNumber


'ÒÀ´ÎÈ¡³öÐÐÄÚÈݲ¢Ð´ÈëdataÊý×éÖÐ
For i = 1 To Hexfile_RowCount '´ÓµÚÒ»ÐÐÈ¡µ½×îºóÒ»ÐÐ
DoEvents
ttext = ""
Temp_Chenksum_PreRow = 0 '±äÁ¿ÇåÁã
'¶ÁÈ¡Ò»ÐÐÊý¾Ý
Line Input #1, ttext

'·Ö½âΪByteÊý¾Ý
Max_Byte_PreRow = (Len(ttext) - 1) / 2 '½«Ò»ÐÐÊý¾Ý¼õȥǰÃæµÄðºÅ£¬È»ºó³ýÒÔ2£¬¾ÍÊǸÃÐеÄbyteÊý
ReDim Hexfile_Data(Max_Byte_PreRow - 1) 'Öض¨ÒåÊý×é

For j = 0 To Max_Byte_PreRow - 1
Hexfile_Data(j) = CByte("&H" & Mid(ttext, (j + 1) * 2, 2))
If j < Max_Byte_PreRow - 1 Then Temp_Chenksum_PreRow = Temp_Chenksum_PreRow + Hexfile_Data(j) '×îºóÒ»¸öbyte²»¼ÆÈëCheckSum
Next j
'¼ÆËãCheckSum
If Temp_Chenksum_PreRow Mod 256 <> 0 Then
Temp_Chenksum_PreRow = 256 - (Temp_Chenksum_PreRow Mod 256) '¼ÆËãchecksum
Else
Temp_Chenksum_PreRow = &H0
End If
'УÑéCheckSum
If Temp_Chenksum_PreRow <> Hexfile_Data(Max_Byte_PreRow - 1) Then
x = MsgBox("ÎļþµÚ " & i & " ÐÐË𻵣¬ÎÞ·¨Í¨¹ýChecksumУÑé¡£", 48, "ÎļþËð»µ")
Close #1
Open_Hex_File = False
Exit Function
End If

'È¡Êý¾ÝÀàÐÍ
'00 Êý¾Ý¼Ç¼ (data record) £ £ £ £ 
'01 ½áÊø¼Ç¼ (end record) £ £ £ £ 
'02 ¶Î¼Ç¼ (paragraph record) £ £ £ £ 
'03 תÒƵØÖ·¼Ç¼ (transfer address record)
If Hexfile_Data(RecordType) = &H1 Then
Exit For
End If

'È¡Îļþ³¤¶È
Hexfile_Datalenght_PreRow = Hexfile_Data(RecordLenght)
' 'È¡ÎļþÆðʼλÖÃ
HexFile_StartAddress = CLng(Hexfile_Data(LoadOffset_H)) * 256 + Hexfile_Data(LoadOffset_L)
'ȡÿÐÐÎļþµÄÊý¾Ý
For j = 0 To Hexfile_Datalenght_PreRow - 1
DoEvents
'ÒÔROMµØַΪÊ×µØÖ·£¬Ð´ÈëÒ»ÐÐÊý¾Ý
Hex_File_Data_Buffer(HexFile_StartAddress + j) = Hexfile_Data(j + 4)
'±È½Ïµ±Ç°µØÖ·ÊÇ·ñµØÖ·×î¸ßλ
If HexFile_StartAddress + j > Temp_Add_Max Then
Temp_Add_Max = HexFile_StartAddress + j
End If
Next j

Next i
Close #tFileNumber '¹Ø±ÕÎļþ
'
' '½«Îļþ³¤¶ÈÏÔʾÔÚ״̬ÖÐ
HexFile_Lenght = Temp_Add_Max + 1 'HexÎļþ³¤¶È£½Hex data address ×î¸ßλ £« 1 £¨ÒòΪhex data address´Ó 0 ¿ªÊ¼£©
lab_HexLenght.Caption = HexFile_Lenght & " Bytes"

Open_Hex_File = True
Exit Function

Error_Handel:
MsgBox Err.Description
Close #tFileNumber
Open_Hex_File = False
End Function

Function TextGetByFile(ByVal pFileName As String) As String
'´ÓÎļþ»ñµÃ¶àÐÐÎı¾
On Error Resume Next
Dim a As Integer
Dim tOutText As String
Dim tFileNumber As Integer
Dim tBytes() As Byte
tFileNumber = FreeFile
Open pFileName For Binary As #tFileNumber
If CBool(LOF(tFileNumber)) Then
ReDim tBytes(LOF(tFileNumber) - 1)
Get tFileNumber, 1, tBytes()
tOutText = StrConv(tBytes, vbUnicode)
Else
a = MsgBox("¾¯¸æ£ºÎļþ¡®" & pFileName & "¡¯Êǿյģ¡", 48, "ÎļþÎÞÄÚÈÝ")
End If
Close #tFileNumber
TextGetByFile = tOutText
End Function
...全文
342 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
清晨曦月 2010-08-15
  • 打赏
  • 举报
回复
文件操作这块从VB6到.NET改变很多。VB6的用起来非常繁琐,自己看看.NET的操作方法吧。
兔子-顾问 2010-08-13
  • 打赏
  • 举报
回复
你这是vb6的代码。不是vb.net的。
你想实现什么?

16,553

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧