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
更多精彩
赞助商链接