一个有关替换二进制文件中某部分内容的问题!急用!!

linton 2005-10-12 08:28:29
有一个二进制文件,其中包含日期数据(如2002年06月),现在想其中的某日期替换为另外的日期(如2005年10月)。开始我想将该二进制文件用Binary方式打开,然后再将其存放在Byte数组中,再转换成字符串,查找字符串并替换,再将替换好的字符串转换成Byte数组,然后再写入另一文件中。结果在将Byte数组转换为字符串时,乱码,不知道如何解决。向大家请教,有什么好的思路和方法,最好能提供源码。谢谢!!
...全文
330 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
supergreenbean 2005-10-13
  • 打赏
  • 举报
回复
你不要把字节数组转换成字符串,你应该把要搜索的字符串也转换成字节数组,然后用InStrB来搜索,这样就不会有问题了
linton 2005-10-13
  • 打赏
  • 举报
回复
更正一下:
For j = 1 To n
DoEvents
strT = strT & ChrB(pBytes(j))
Next j
linton 2005-10-13
  • 打赏
  • 举报
回复
昨天我也试过了你这种方法,没有成功。我用下面的代码也未成功实现,不知是何原因。代码如下:

Dim n As Long,j as Long,i as Long
Dim strT As String
Dim arrBytes() As Byte,arrTBytes() As Byte
Dim tByte As Byte

Open strFileName For Binary As #1
n = LOF(1)
ReDim arrBytes(1 To n) As Byte
Get #1, , arrBytes
Close #1

'第一种转换方法
strT=""
strT = StrConv(arrBytes, vbUnicode)
strT = Left$(strT, n)
'第二种转换方法
strT=""
For j = 1 To n
DoEvents
strT = strT & ChrB(pBytes(tLoop))
Next tLoop

……
strT = Replace(strT, "2002年10月", "2005年5月")
strT = Replace(strT, "2003年6月", "2005年10月")
strT = Replace(strT, "2003年7月", "2005年10月")
……

'第一种方法
arrTBytes= StrConv(strT, vbFromUnicode)
'第二种方法
i = LenB(strT)
For j= 1 To i
DoEvents
tByte = AscB(MidB(strT, j, 1))
ReDim Preserve arrTBytes(j - 1)
arrTBytes(j - 1) = tByte
Next j

……
Open strOutFileName For Binary As #1
Put #1, , arrTBytes
Close #1
……

结果按照第一种方法将Byte数组转换后,strT能识别部分中文(如2002年10月、2003年6月、2003年7月等等),而用第二种方法将Byte数组转换后,strT全部乱码。另外,按照第一种方法将字符串转换成Byte数组后再保存结果,发现保存后的文件大小比原文件小几K,按照第二种方法将字符串转换成Byte数组后再保存结果,发现保存后的文件大小几乎是原文件的两倍。
请问这是由什么原因引起的,针对这一问题,我该怎么修改才能达到预定的要求。谢谢赐教!急用!!
supergreenbean 2005-10-13
  • 打赏
  • 举报
回复
Option Explicit
Option Base 0

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

Function FindAndReplace(ByVal sFilename As String, ByVal sFindText As String, ByVal sReplaceText As String, Optional ByVal lStartPos As Long = 1) As Long
Dim abFindText() As Byte, abReplaceText() As Byte
abFindText = sFindText
abReplaceText = sReplaceText
FindAndReplace = FindAndReplaceB(sFilename, abFindText(), abReplaceText(), lStartPos)
End Function

Function FindAndReplaceB(ByVal sFilename As String, abFindText() As Byte, abReplaceText() As Byte, Optional ByVal lStartPos As Long = 1) As Long
On Error Resume Next
Dim abTempFileBuffer() As Byte, abFileBuffer() As Byte
Dim lFindTextLen As Long, lReplaceTextLen As Long, lFileLen As Long
Dim lFreeFile As Long

FindAndReplaceB = -1

lFindTextLen = UBound(abFindText) + 1

lReplaceTextLen = UBound(abReplaceText) + 1

lFreeFile = FreeFile
Open sFilename For Binary As lFreeFile

lFileLen = LOF(lFreeFile)

If lFileLen > 0 Then
ReDim abTempFileBuffer(lFileLen - 1)
Get lFreeFile, , abTempFileBuffer

ReDim abFileBuffer(lFileLen + (lReplaceTextLen - lFindTextLen) - 1)
End If

Close lFreeFile

If Err.Number <> 0 Or FileLen = 0 Then Exit Function

FindAndReplaceB = InStrB(lStartPos, abTempFileBuffer, abFindText, vbBinaryCompare)
If FindAndReplaceB > 0 Then
If FindAndReplaceB > 1 Then
CopyMemory abFileBuffer(0), abTempFileBuffer(0), FindAndReplaceB - 1
End If

CopyMemory abFileBuffer(FindAndReplaceB - 1), abReplaceText(0), lReplaceTextLen

CopyMemory abFileBuffer(FindAndReplaceB + lReplaceTextLen - 1), abTempFileBuffer(FindAndReplaceB + lFindTextLen - 1), lFileLen - FindAndReplaceB - lFindTextLen + 1

lFreeFile = FreeFile
Kill sFilename
Open sFilename For Binary As lFreeFile
Put lFreeFile, 1, abFileBuffer
Close lFreeFile
End If
End Function

Private Sub Form_Load()
'Open "c:\testfsd.txt" For Binary As #1
'Dim b() As Byte
'b = String$(12, 11) & "2005年6月" & vbTab
'Put #1, , b
'Close #1
Debug.Print "替换位置在:"; FindAndReplace("c:\testfsd.txt", "2005年6月", "2005年12月")
End Sub
linton 2005-10-13
  • 打赏
  • 举报
回复
高手们,还有没有其他的方法啊?
linton 2005-10-13
  • 打赏
  • 举报
回复
刚才试着用InStrB查找,但是发现查找结果为0。
northwolves 2005-10-12
  • 打赏
  • 举报
回复
根据日期字段结构长度,逐字节方式替换

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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