如何在VbScript中创建BinaryArray?

时间:2022-12-02 21:05:53

I want to manually create a binary script and then save it as binary file.

我想手动创建二进制脚本,然后将其保存为二进制文件。

I want to append all of the following bytes and create a binary file out of them.

我想追加所有以下字节并从中创建一个二进制文件。

&HF0
&HF1
&HF2

I want to able to do something like this :

我希望能够做到这样的事情:

Dim generateData(3) As Byte
generateData(0) = &HFF
generateData(1) = &HFE
generateData(2) = &HFC

But obviously As Byte doesn't work on Vbscript. I do use the following function to write binary array to disk (at least I'll when I able to create a binary array)

但显然As Byte不适用于Vbscript。我使用以下函数将二进制数组写入磁盘(至少我能够创建二进制数组时)

Function SaveBinaryData(FileName, ByteArray)
  Const adTypeBinary = 1
  Const adSaveCreateOverWrite = 2

  'Create Stream object
  Dim BinaryStream
  Set BinaryStream = CreateObject("ADODB.Stream")

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

  'Open the stream And write binary data To the object
  BinaryStream.Open
  BinaryStream.Write ByteArray

  'Save binary data To disk
  BinaryStream.SaveToFile FileName, adSaveCreateOverWrite
End Function

1 个解决方案

#1


I have seen something along these lines used:

我看到过这些方面的用法:

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

#1


I have seen something along these lines used:

我看到过这些方面的用法:

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