How can I replace byte in byte array?

B

bonnie.tangyn

Dear all

I cannot replace all matched byte using replace function. The function
seems didn't compare all lines. When I output the outtemplate.h5t, I
found the SID1---- isn't replaced by 60083854. So does SID2.

Can you give me some advices on my coding?

Here is my coding:
Sub ProcessBinaryFile()
Dim inFh As Integer, outFh As Integer, cctFh As Integer,
intReplacePos As Integer, intFindPos As Integer, intPos As Long
Dim cctBuffer As String, strFind As String, strReplace As String
Dim filebytes() As Byte, byteFind() As Byte, byteReplace() As Byte

inFh = FreeFile
Open "c:\template5.h5t" For Binary Access Read Lock Read Write As
#inFh
ReDim filebytes(1 To LOF(inFh))
Get #inFh, , filebytes()
Close #inFh

cctFh = FreeFile
Open "c:\StringsCompareReplaceFile.dat" For Input As #cctFh
Do Until (EOF(cctFh))
Line Input #cctFh, cctBuffer

If InStr(cctBuffer, Chr(9)) > 0 Then

intFindPos = InStr(cctBuffer, Chr(9))
strFind = Mid$(cctBuffer, 1, intFindPos - 1)
byteFind = StrConv(strFind, vbFromUnicode)

intReplacePos = InStrRev(cctBuffer, Chr(9)) + 1
strReplace = Mid$(cctBuffer, intReplacePos)
byteReplace = StrConv(strReplace, vbFromUnicode)

filebytes() = Replace(filebytes(), byteFind, byteReplace, , ,
vbBinaryCompare)
End If
Loop
Close #cctFh

outFh = FreeFile
Open "c:\outtemplate.h5t" For Binary Access Write Lock Read Write
As #outFh
Put #outFh, , filebytes()
Close #outFh
End Sub

StringCompareReplaceFile is like:
STUDENTNAME1-------------------------------------- Bart
Simpson-----------------------------------
SID1---- 60083854
STUDENTNAME2-------------------------------------- Lisa
Simpson-----------------------------------
LID2---- 60033953
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top