1

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)

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
dr. evil
  • 26,944
  • 33
  • 131
  • 201

2 Answers2

3

The using a TypeText-ADODB.Stream to write a file and read it back as TypeBinary-ADODB.Stream is a Common solution, but it requires a Temp file and Text safe bytes. There is a better way using System.IO.MemoryStream

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

Set mem = CreateObject("System.IO.MemoryStream")
mem.SetLength(0)
For Each B in generateData 
   mem.WriteByte (B)
Next
generateData_as_ByteArray = mem.ToArray()  'Returns a VB Style, "Byte()"   [Byte Array]

Or in the Question's Original Example

Function SaveBinaryData(FileName, VbByteArray)
  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

  'Covert VBS Array of Byte Values; TypeName "Variant()"
  'to a true Byte Array; TypeName "Byte()"
  Set mem = CreateObject("System.IO.MemoryStream")
  mem.SetLength(0)
  For Each B in VbByteArray
     mem.WriteByte (B)
  Next
  ByteArray = mem.ToArray() 'Output as Btye()

  'Works
  BinaryStream.Write ByteArray

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

An ADODB.Stream (in Binary Mode) only lets you add Arrays of Bytes, but the closely related System.IO.MemoryStream also lets you add Data as Arrays of Bytes, however has the .WriteByte function which takes just a single byte, which VBS can do.

System.IO.MemoryStream's .ToArray returns a ByteArray the same as .read does with ADODB.Stream; both of which is the type "byte()" that ADODB.Stream .Write wants.

  • It works for me! I used to get "byte()" by using `ADODB.Stream` to read a prepared file which contains 256 bytes from 0x00 to 0xFF. – cuixiping May 11 '23 at 09:24
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
Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541