Private Function SaveByteBufferToFile(ByVal vFileName As String, _ ByVal vBufferLength As Long, _ Optional ByVal vHeadString As String = "") As Boolean Dim i As Long Dim o_SaveFile As Long Dim o_strInfo As String o_SaveFile = FreeFile Open vFileName For Output As #o_SaveFile If Len(vHeadString) > 0 Then Print #o_SaveFile, vHeadString End If GetMemoryData 0, vBufferLength, o_strInfo Print #o_SaveFile, o_strInfo Print #o_SaveFile, vbCrLf & vbCrLf & vbCrLf Close #o_SaveFile SaveByteBufferToFile = True End FunctionPrivate Function GetMemoryData(ByVal vBaseADR As Long, _ ByVal vLength As Long, _ ByRef vBuffer As String, _ Optional ByVal vNeedLine As Boolean = True, _ Optional ByVal vLineLength As Long = 16, _ Optional ByVal vExchangeHighLowBit As Boolean = False) As Boolean Dim o_iBaseADR As Long Dim o_iLength As Long Dim o_strBuffer As String Dim i As Long Dim j As Long Dim o_strLINE As String
o_iBaseADR = vBaseADR o_iLength = vLength vBuffer = "" If o_iBaseADR < 0 Then o_iBaseADR = 0 If o_iLength <= 0 Then Exit Function
Dim o_iSegment As Long Dim o_iCount As Long o_iCount = 0 o_iSegment = vBaseADR
Dim o_iWorkBit As Long
For i = vBaseADR To vBaseADR + vLength - 1 If vExchangeHighLowBit Then '高低位互换 o_iWorkBit = IIf(i Mod 2 = 0, i + 1, i - 1) Else o_iWorkBit = i End If
If vNeedLine Then '分行 If o_iCount Mod vLineLength = 0 Then vBuffer = vBuffer & ChangeByteToHex(i) & ": " End If End If
vBuffer = vBuffer & ChangeByteToHex(m_byteResult(o_iWorkBit), , 2) & " " If vNeedLine Then '分行 If (o_iCount Mod vLineLength) = (vLineLength - 1) Then vBuffer = vBuffer & " " For j = o_iSegment To o_iSegment + vLineLength - 1 If vExchangeHighLowBit Then '高低位互换 o_iWorkBit = IIf(j Mod 2 = 0, j + 1, j - 1) Else o_iWorkBit = j End If vBuffer = vBuffer & ChangeByteToChar(m_byteResult(o_iWorkBit)) Next j vBuffer = vBuffer & vbCrLf o_iSegment = o_iSegment + vLineLength End If End If o_iCount = o_iCount + 1 Next i GetMemoryData = True End Function Private Function ChangeByteToHex(ByVal vData As Long, _ Optional ByVal vHex As Long = 16, _ Optional ByVal vLoopEnd As Long = 4) As String Dim o_iData As Long Dim o_iBit As Long Dim o_strEnd As String Dim i As Long Dim o_strHEX(0 To 15) As String o_strHEX(0) = "0" o_strHEX(1) = "1" o_strHEX(2) = "2" o_strHEX(3) = "3" o_strHEX(4) = "4" o_strHEX(5) = "5" o_strHEX(6) = "6" o_strHEX(7) = "7" o_strHEX(8) = "8" o_strHEX(9) = "9" o_strHEX(10) = "A" o_strHEX(11) = "B" o_strHEX(12) = "C" o_strHEX(13) = "D" o_strHEX(14) = "E" o_strHEX(15) = "F" o_iData = vData If o_iData < 0 Then o_iData = o_iData + 65535 o_strEnd = "" For i = 1 To vLoopEnd o_iBit = o_iData Mod vHex o_strEnd = o_strHEX(o_iBit) & o_strEnd o_iData = o_iData \ vHex Next i ChangeByteToHex = o_strEnd End Function Private Function ChangeByteToChar(ByVal vChar As Long, _ Optional ByVal vElseChar As String = ".") As String Select Case vChar Case 0: ChangeByteToChar = " " Case 32 To 126: ChangeByteToChar = Chr(vChar) Case Else: ChangeByteToChar = vElseChar End Select End Function '------------------------------------------------------------------------------------- ' 存储错误数据到文本文件中 ' vSource As String 错误来源 ' vErrNumber As Long 错误号码 ' vErrDescription As String 错误描述 ' Optional vExInformation As String = "" 附加的信息 Public Sub SaveErrorInfo(ByVal vSource As String, _ ByVal vErrNumber As Long, _ ByVal vErrDescription As String, _ Optional ByVal vExInformation As String = "") Dim o_SaveFile As Long Dim o_strErrMessage As String Static o_iCount As Long o_iCount = o_iCount + 1 o_SaveFile = FreeFile Open App.Path & "\error.txt" For Append As #o_SaveFile o_strErrMessage = vbCrLf & "---------------------------------------------------" & _ vbCrLf & Now & _ vbCrLf & " ①操作者ID:" & "sa" & " 名字:" & "sa" & _ vbCrLf & " ②错误来源:" & vSource & _ vbCrLf & " ③错误号码:" & vErrNumber & _ vbCrLf & " ④错误描述:" & vErrDescription & _ vbCrLf & " ⑤其他信息:" & vExInformation & _ vbCrLf & " ⑥错误计数器: " & o_iCount Print #o_SaveFile, o_strErrMessage Close #o_SaveFile End Sub'在指定的文件中搜索特定的字符串,输出到指定的文件 Private Function SearchBytesInString(ByVal vFileName As String, _ ByVal vTargetBitString As String, _ Optional ByVal vResultFileName As String = "SearchOut.txt") As Boolean SearchBytesInString = False If Len(vFileName) = 0 Then Exit Function If Len(vTargetBitString) = 0 Then Exit Function
Dim o_byteSearch() As Byte o_byteSearch = Split(vTargetBitString, " ", , vbTextCompare)
SearchBytesInString = True End FunctionPrivate Function ChangeHexToLong(ByVal vHex As String) As Byte ChangeHexToLong = 0 Dim o_strBit As String Dim o_iTotal As Long Dim i As Long o_iTotal = 0 For i = 1 To 2 o_strBit = Mid(vHex, i, 1) o_iTotal = o_iTotal * 16 + GetBitHexValue(o_strBit) Next i ChangeHexToLong = CByte(o_iTotal) End Function Private Function GetBitHexValue(ByVal vHexBIT As String) As Long Select Case UCase(vHexBIT) Case "0": GetBitHexValue = 0 Case "1": GetBitHexValue = 1 Case "2": GetBitHexValue = 2 Case "3": GetBitHexValue = 3 Case "4": GetBitHexValue = 4 Case "5": GetBitHexValue = 5 Case "6": GetBitHexValue = 6 Case "7": GetBitHexValue = 7 Case "8": GetBitHexValue = 8 Case "9": GetBitHexValue = 9 Case "A": GetBitHexValue = 10 Case "B": GetBitHexValue = 11 Case "C": GetBitHexValue = 12 Case "D": GetBitHexValue = 13 Case "E": GetBitHexValue = 14 Case "F": GetBitHexValue = 15 Case Else: GetBitHexValue = -1 End Select End Function
Private Const mc_iBufferLength As Long = 1024 Private m_byteResult(0 To mc_iBufferLength) As BytePublic Sub main() ' SetFileBin3A App.Path & "\data.dat" ManyFileComp End Sub
'功能:比较多个文件中相同的部分。 Public Function ManyFileComp() As Boolean
Dim i As Long For i = 0 To mc_iBufferLength m_byteResult(i) = &HFF Next i
Dim oFileName0000 As String Dim oFileName0001 As String Dim oFileName0010 As String Dim oFileName0100 As String Dim oFileName1000 As String Dim oFileName1001 As String Dim oFileName1010 As String Dim oFileName1100 As String Dim oFileName0101 As String Dim oFileName0110 As String Dim oFileName0011 As String Dim oFileName1110 As String Dim oFileName1101 As String Dim oFileName1011 As String Dim oFileName0111 As String Dim oFileName1111 As String
Dim oFileNameB00 As String Dim oFileNameB01 As String Dim oFileNameB10 As String Dim oFileNameB11 As String
Dim oiCounter As Long Dim oiCounter44 As Long oiCounter = 0 oiCounter44 = 0 For i = 0 To mc_iBufferLength If m_byteResult(i) > 0 Then oiCounter = oiCounter + 1 End If If m_byteResult(i) = &H88 Then oiCounter44 = oiCounter44 + 1 End If Next i MsgBox oiCounter & " - " & oiCounter - oiCounter44 & " = " & oiCounter44 & "[&H44]"
Dim oHandleResult As Long oHandleResult = FreeFile Open oFileNameResult For Binary As #oHandleResult Put #oHandleResult, , m_byteResult Close #oHandleResult End Function
Private Function SaveByteBufferToFile(ByVal vFileName As String, _
ByVal vBufferLength As Long, _
Optional ByVal vHeadString As String = "") As Boolean
Dim i As Long
Dim o_SaveFile As Long
Dim o_strInfo As String
o_SaveFile = FreeFile
Open vFileName For Output As #o_SaveFile
If Len(vHeadString) > 0 Then
Print #o_SaveFile, vHeadString
End If
GetMemoryData 0, vBufferLength, o_strInfo
Print #o_SaveFile, o_strInfo
Print #o_SaveFile, vbCrLf & vbCrLf & vbCrLf
Close #o_SaveFile
SaveByteBufferToFile = True
End FunctionPrivate Function GetMemoryData(ByVal vBaseADR As Long, _
ByVal vLength As Long, _
ByRef vBuffer As String, _
Optional ByVal vNeedLine As Boolean = True, _
Optional ByVal vLineLength As Long = 16, _
Optional ByVal vExchangeHighLowBit As Boolean = False) As Boolean
Dim o_iBaseADR As Long
Dim o_iLength As Long
Dim o_strBuffer As String
Dim i As Long
Dim j As Long
Dim o_strLINE As String
o_iBaseADR = vBaseADR
o_iLength = vLength
vBuffer = ""
If o_iBaseADR < 0 Then o_iBaseADR = 0
If o_iLength <= 0 Then Exit Function
Dim o_iSegment As Long
Dim o_iCount As Long
o_iCount = 0
o_iSegment = vBaseADR
Dim o_iWorkBit As Long
For i = vBaseADR To vBaseADR + vLength - 1
If vExchangeHighLowBit Then '高低位互换
o_iWorkBit = IIf(i Mod 2 = 0, i + 1, i - 1)
Else
o_iWorkBit = i
End If
If vNeedLine Then '分行
If o_iCount Mod vLineLength = 0 Then
vBuffer = vBuffer & ChangeByteToHex(i) & ": "
End If
End If
vBuffer = vBuffer & ChangeByteToHex(m_byteResult(o_iWorkBit), , 2) & " "
If vNeedLine Then '分行
If (o_iCount Mod vLineLength) = (vLineLength - 1) Then
vBuffer = vBuffer & " "
For j = o_iSegment To o_iSegment + vLineLength - 1
If vExchangeHighLowBit Then '高低位互换
o_iWorkBit = IIf(j Mod 2 = 0, j + 1, j - 1)
Else
o_iWorkBit = j
End If
vBuffer = vBuffer & ChangeByteToChar(m_byteResult(o_iWorkBit))
Next j
vBuffer = vBuffer & vbCrLf
o_iSegment = o_iSegment + vLineLength
End If
End If
o_iCount = o_iCount + 1
Next i
GetMemoryData = True
End Function
Private Function ChangeByteToHex(ByVal vData As Long, _
Optional ByVal vHex As Long = 16, _
Optional ByVal vLoopEnd As Long = 4) As String
Dim o_iData As Long
Dim o_iBit As Long
Dim o_strEnd As String
Dim i As Long
Dim o_strHEX(0 To 15) As String
o_strHEX(0) = "0"
o_strHEX(1) = "1"
o_strHEX(2) = "2"
o_strHEX(3) = "3"
o_strHEX(4) = "4"
o_strHEX(5) = "5"
o_strHEX(6) = "6"
o_strHEX(7) = "7"
o_strHEX(8) = "8"
o_strHEX(9) = "9"
o_strHEX(10) = "A"
o_strHEX(11) = "B"
o_strHEX(12) = "C"
o_strHEX(13) = "D"
o_strHEX(14) = "E"
o_strHEX(15) = "F"
o_iData = vData
If o_iData < 0 Then o_iData = o_iData + 65535
o_strEnd = ""
For i = 1 To vLoopEnd
o_iBit = o_iData Mod vHex
o_strEnd = o_strHEX(o_iBit) & o_strEnd
o_iData = o_iData \ vHex
Next i
ChangeByteToHex = o_strEnd
End Function
Private Function ChangeByteToChar(ByVal vChar As Long, _
Optional ByVal vElseChar As String = ".") As String
Select Case vChar
Case 0: ChangeByteToChar = " "
Case 32 To 126: ChangeByteToChar = Chr(vChar)
Case Else: ChangeByteToChar = vElseChar
End Select
End Function
'-------------------------------------------------------------------------------------
' 存储错误数据到文本文件中
' vSource As String 错误来源
' vErrNumber As Long 错误号码
' vErrDescription As String 错误描述
' Optional vExInformation As String = "" 附加的信息
Public Sub SaveErrorInfo(ByVal vSource As String, _
ByVal vErrNumber As Long, _
ByVal vErrDescription As String, _
Optional ByVal vExInformation As String = "")
Dim o_SaveFile As Long
Dim o_strErrMessage As String
Static o_iCount As Long
o_iCount = o_iCount + 1
o_SaveFile = FreeFile
Open App.Path & "\error.txt" For Append As #o_SaveFile
o_strErrMessage = vbCrLf & "---------------------------------------------------" & _
vbCrLf & Now & _
vbCrLf & " ①操作者ID:" & "sa" & " 名字:" & "sa" & _
vbCrLf & " ②错误来源:" & vSource & _
vbCrLf & " ③错误号码:" & vErrNumber & _
vbCrLf & " ④错误描述:" & vErrDescription & _
vbCrLf & " ⑤其他信息:" & vExInformation & _
vbCrLf & " ⑥错误计数器: " & o_iCount
Print #o_SaveFile, o_strErrMessage
Close #o_SaveFile
End Sub'在指定的文件中搜索特定的字符串,输出到指定的文件
Private Function SearchBytesInString(ByVal vFileName As String, _
ByVal vTargetBitString As String, _
Optional ByVal vResultFileName As String = "SearchOut.txt") As Boolean
SearchBytesInString = False
If Len(vFileName) = 0 Then Exit Function
If Len(vTargetBitString) = 0 Then Exit Function
Dim o_byteSearch() As Byte
o_byteSearch = Split(vTargetBitString, " ", , vbTextCompare)
SearchBytesInString = True
End FunctionPrivate Function ChangeHexToLong(ByVal vHex As String) As Byte
ChangeHexToLong = 0
Dim o_strBit As String
Dim o_iTotal As Long
Dim i As Long
o_iTotal = 0
For i = 1 To 2
o_strBit = Mid(vHex, i, 1)
o_iTotal = o_iTotal * 16 + GetBitHexValue(o_strBit)
Next i
ChangeHexToLong = CByte(o_iTotal)
End Function
Private Function GetBitHexValue(ByVal vHexBIT As String) As Long
Select Case UCase(vHexBIT)
Case "0": GetBitHexValue = 0
Case "1": GetBitHexValue = 1
Case "2": GetBitHexValue = 2
Case "3": GetBitHexValue = 3
Case "4": GetBitHexValue = 4
Case "5": GetBitHexValue = 5
Case "6": GetBitHexValue = 6
Case "7": GetBitHexValue = 7
Case "8": GetBitHexValue = 8
Case "9": GetBitHexValue = 9
Case "A": GetBitHexValue = 10
Case "B": GetBitHexValue = 11
Case "C": GetBitHexValue = 12
Case "D": GetBitHexValue = 13
Case "E": GetBitHexValue = 14
Case "F": GetBitHexValue = 15
Case Else: GetBitHexValue = -1
End Select
End Function
Private m_byteResult(0 To mc_iBufferLength) As BytePublic Sub main()
' SetFileBin3A App.Path & "\data.dat"
ManyFileComp
End Sub
'功能:比较多个文件中相同的部分。
Public Function ManyFileComp() As Boolean
Dim i As Long
For i = 0 To mc_iBufferLength
m_byteResult(i) = &HFF
Next i
Dim oFileName0000 As String
Dim oFileName0001 As String
Dim oFileName0010 As String
Dim oFileName0100 As String
Dim oFileName1000 As String
Dim oFileName1001 As String
Dim oFileName1010 As String
Dim oFileName1100 As String
Dim oFileName0101 As String
Dim oFileName0110 As String
Dim oFileName0011 As String
Dim oFileName1110 As String
Dim oFileName1101 As String
Dim oFileName1011 As String
Dim oFileName0111 As String
Dim oFileName1111 As String
Dim oFileNameB00 As String
Dim oFileNameB01 As String
Dim oFileNameB10 As String
Dim oFileNameB11 As String
Dim oFileNameResult As String
oFileName1111 = App.Path & "\..\a\a1.VSD"
oFileName0000 = App.Path & "\..\a\a2.VSD"
oFileName0001 = App.Path & "\..\a\a3.VSD"
oFileName0010 = App.Path & "\..\a\a4.VSD"
oFileName0100 = App.Path & "\..\a\a5.VSD"
oFileName1000 = App.Path & "\..\a\a6.VSD"
oFileName1001 = App.Path & "\..\a\a7.VSD"
oFileName1010 = App.Path & "\..\a\a8.VSD"
oFileName1100 = App.Path & "\..\a\a9.VSD"
oFileName0101 = App.Path & "\..\b\b1.VSD"
oFileName0110 = App.Path & "\..\b\b2.VSD"
oFileName0011 = App.Path & "\..\b\b3.VSD"
oFileName1110 = App.Path & "\..\b\b4.VSD"
oFileName1101 = App.Path & "\..\b\b5.VSD"
oFileName1011 = App.Path & "\..\b\b6.VSD"
oFileName0111 = App.Path & "\..\c\c1.VSD"
oFileNameB00 = App.Path & "\..\c\c2.VSD"
oFileNameB01 = App.Path & "\..\c\c3.VSD"
oFileNameB10 = App.Path & "\..\c\c4.VSD"
oFileNameB11 = App.Path & "\..\c\c5.VSD"
oFileNameResult = App.Path & "\result.dat"
TwoFileComp oFileName1111, oFileName0001
TwoFileComp oFileName1111, oFileName0010
TwoFileComp oFileName1111, oFileName0100
TwoFileComp oFileName1111, oFileName1000
TwoFileComp oFileName1111, oFileName1001
TwoFileComp oFileName1111, oFileName1010
TwoFileComp oFileName1111, oFileName1100
TwoFileComp oFileName1111, oFileName0101
TwoFileComp oFileName1111, oFileName0110
TwoFileComp oFileName1111, oFileName0011
TwoFileComp oFileName1111, oFileName1110
TwoFileComp oFileName1111, oFileName1101
TwoFileComp oFileName1111, oFileName1011
TwoFileComp oFileName1111, oFileName0111
TwoFileComp oFileName1111, oFileNameB00
TwoFileComp oFileName1111, oFileNameB01
TwoFileComp oFileName1111, oFileNameB10
TwoFileComp oFileName1111, oFileNameB11
'Dim oFileNameResultA As String
'Dim oFileNameResultB As String
'oFileNameResultA = App.Path & "\resultA.dat"
'oFileNameResultB = App.Path & "\resultB.dat"
'TwoFileComp oFileNameResultA, oFileNameResultB, False, False
Dim oiCounter As Long
Dim oiCounter44 As Long
oiCounter = 0
oiCounter44 = 0
For i = 0 To mc_iBufferLength
If m_byteResult(i) > 0 Then
oiCounter = oiCounter + 1
End If
If m_byteResult(i) = &H88 Then
oiCounter44 = oiCounter44 + 1
End If
Next i
MsgBox oiCounter & " - " & oiCounter - oiCounter44 & " = " & oiCounter44 & "[&H44]"
SaveByteBufferToFile App.Path & "\dump.txt", mc_iBufferLength, oiCounter & " - " & oiCounter - oiCounter44 & " = " & oiCounter44 & "[&H44]" & vbCrLf
Dim oHandleResult As Long
oHandleResult = FreeFile
Open oFileNameResult For Binary As #oHandleResult
Put #oHandleResult, , m_byteResult
Close #oHandleResult
End Function