【问题标题】:Search And Replace within A Binary Stream在二进制流中搜索和替换
【发布时间】:2015-05-19 09:04:30
【问题描述】:

我正在尝试更改字节流数组中的值。我正在寻找 Null 值,我想将其更改为空格。当我尝试访问该数组时,我收到一条错误消息“类型不匹配”。

我的 VBS 代码:

Const adTypeBinary = 1
Const adSaveCreateOverWrite = 2
Const adSaveCreateNotExist=1
'Create Stream object
Dim BinaryStream
Set BinaryStream = CreateObject("ADODB.Stream")
Dim InputFile
InputFile="C:\Users\oferbe\Documents\Tfachut\prepr\Testinput.txt"

'Specify stream type - we want To get binary data.
BinaryStream.Type = adTypeBinary

'Open the stream
BinaryStream.Open
'Load the file data from disk To stream object
BinaryStream.LoadFromFile InputFile

'Open the stream And get binary data from the object
ReadBinaryFile = BinaryStream.Read
BinaryStream.Close

For i = 0 to UBound(ReadBinaryFile)
  If ReadBinaryFile(i)=00 Then ReadBinaryFile(i)=20
Next

BinaryStream.Open
'BinaryStream.Write ByteArray
BinaryStream.Write ReadBinaryFile
Dim OutPutFile
OutPutFile="C:\Users\oferbe\Documents\Tfachut\prepr\Ofer"
'Save binary data To disk
BinaryStream.SaveToFile OutPutFile, adSaveCreateOverWrite

【问题讨论】:

    标签: vbscript binarystream


    【解决方案1】:

    Read 操作返回一个字节数组,它基本上是一个二进制字符串,具有 VBScript 数组的一些属性,但不是全部。您最好将二进制流作为常规字符串读取:

    inputFile  = "C:\path\to\your\input.bin"
    outputFile = "C:\path\to\your\output.bin"
    
    Set stream = CreateObject("ADODB.Stream")
    stream.Open
    stream.Type = 2
    stream.Charset = "Windows-1252"
    stream.LoadFromFile inputFile
    data = stream.ReadText
    stream.Close
    
    data = Replace(data, Chr(0), Chr(32))
    
    stream.Open
    stream.Type = 2
    stream.Charset = "Windows-1252"
    stream.WriteText data
    stream.SaveToFile outputFile, 2
    stream.Close
    
    Set stream = Nothing
    

    【讨论】:

      猜你喜欢
      • 2015-06-19
      • 2019-04-21
      • 2011-03-10
      • 1970-01-01
      • 1970-01-01
      • 2017-01-05
      • 2015-05-11
      • 1970-01-01
      • 2013-10-22
      相关资源
      最近更新 更多