回复 7楼 dx002386
程序代码:
Function ReadBinary(FileName)
Const adTypeBinary = 1
Dim stream, xmldom, node
Set xmldom = CreateObject("Microsoft.XMLDOM")
Set node = xmldom.CreateElement("binary")
node.DataType = "bin.hex"
Set stream = CreateObject("ADODB.Stream")
stream.Type = adTypeBinary
stream.Open
stream.LoadFromFile FileName
node.NodeTypedValue = stream.Read
stream.Close
Set stream = Nothing
ReadBinary = node.Text
Set node = Nothing
Set xmldom = Nothing
End Function
Sub WriteBinary(FileName, Buf)
Const adTypeBinary = 1
Const adSaveCreateOverWrite = 2
Dim stream, xmldom, node
Set xmldom = CreateObject("Microsoft.XMLDOM")
Set node = xmldom.CreateElement("binary")
node.DataType = "bin.hex"
node.Text = Buf
Set stream = CreateObject("ADODB.Stream")
stream.Type = adTypeBinary
stream.Open
stream.write node.NodeTypedValue
stream.SaveToFile FileName, adSaveCreateOverWrite
stream.Close
Set stream = Nothing
Set node = Nothing
Set xmldom = Nothing
End Sub
Private Sub Form_Load()
Dim str As String, i As Long, j As String, k As String
str = UCase(ReadBinary("1.fas")) '获取指定二进制文件的十六进制代码,代码形式:4D5A90000300
'MsgBox str
For i = 1 To Len(str) Step 2
j = Mid(str, i, 2)
k = k + " " + j + " "
Next
k = Right(k, Len(k) - 1)
k = Left(k, Len(k) - 1)
'MsgBox k '获取格式化之后的十六进制代码,代码形式:4D 5A 90 00 03 00
'替换字符串(有多少替换写多少行):
k = Replace(k, "1E 6F 11 55 55", "00 6D 00 00 00") '1E 6F 11 55 55 替换为 00 6D 00 00 00
k = Replace(k, "1E E3 11 55 55", "00 E1 00 00 00") '1E E3 11 55 55 替换为 00 E1 00 00 00
k = Replace(k, " ", "") '写回二进制前,记得要把格式化之后的十六进制代码还原为格式化之前状态才能写入。
WriteBinary "New_1.fas", k '写回二进制文件,文件名若为同名则写入相同文件
End Sub
[此贴子已经被作者于2021-2-10 18:33编辑过]