WEB开发网
开发学院WEB开发ASP VBScript读写二进制文件 阅读

VBScript读写二进制文件

 2012-05-28 09:05:22 来源:WEB开发网   
核心提示:ReadBinary函数从文件读取二进制数据并返回数组,WriteBinary将数组中的数据以二进制的形式写到文件中,VBScript读写二进制文件,Function ReadBinary(FileName) Dim Buf(), I With CreateObject("ADODB.Stream&quo

ReadBinary函数从文件读取二进制数据并返回数组,WriteBinary将数组中的数据以二进制的形式写到文件中。

Function ReadBinary(FileName)
  Dim Buf(), I
  With CreateObject("ADODB.Stream")
    .Mode = 3: .Type = 1: .Open: .LoadFromFile FileName
    ReDim Buf(.Size - 1)
    For I = 0 To .Size - 1: Buf(I) = AscB(.Read(1)): Next
    .Close
  End With
  ReadBinary = Buf
End Function

Sub WriteBinary(FileName, Buf)
  Dim I, aBuf, Size, bStream
  Size = UBound(Buf): ReDim aBuf(Size \ 2)
  For I = 0 To Size - 1 Step 2
      aBuf(I \ 2) = ChrW(Buf(I + 1) * 256 + Buf(I))
  Next
  If I = Size Then aBuf(I \ 2) = ChrW(Buf(I))
  aBuf=Join(aBuf, "")
  Set bStream = CreateObject("ADODB.Stream")
  bStream.Type = 1: bStream.Open
  With CreateObject("ADODB.Stream")
    .Type = 2 : .Open: .WriteText aBuf
    .Position = 2: .CopyTo bStream: .Close
  End With
  bStream.SaveToFile FileName, 2: bStream.Close
  Set bStream = Nothing
End Sub 

Tags:VBScript 读写 二进制

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接