How to write binary data to disk in VBScript?

I have a binary string that I need to write to a file. I have a feeling that this should be a simple procedure, but again, VBScript. FileSystemObject does not help, as it transfers data. The Stream object looks promising, with it the adBinaryMode and its Write method, but the Write method requires a byte array and does not seem to accept an alternative array instead. Since VBScript arrays are all variants of arrays, this seems problematic.

So, how do I just write data to a file?

EDIT: I have to add that all this should be VBScript. No additional components. Sorry, I don't like this either.

+4
source share
3 answers

This is also possible with a regular FileSystemObject , here is the code I use in a custom loading script that I wrote a long time ago using code I found online that converts a binary string to ASCII:

 Set objFSO = Server.CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.CreateTextFile("file path here") objFile.Write(RSBinaryToString(strBinaryContents)) objFile.Close Set objFile=Nothing Set objFSO=Nothing Private Function RSBinaryToString(xBinary) 'Antonin Foller, http://www.motobit.com 'RSBinaryToString converts binary data (VT_UI1 | VT_ARRAY Or MultiByte string) 'to a string (BSTR) using ADO recordset Dim Binary 'MultiByte data must be converted To VT_UI1 | VT_ARRAY first. If vartype(xBinary)=8 Then Binary = MultiByteToBinary(xBinary) Else Binary = xBinary Dim RS, LBinary Const adLongVarChar = 201 Set RS = CreateObject("ADODB.Recordset") LBinary = LenB(Binary) If LBinary>0 Then RS.Fields.Append "mBinary", adLongVarChar, LBinary RS.Open RS.AddNew RS("mBinary").AppendChunk Binary RS.Update RSBinaryToString = RS("mBinary") Else RSBinaryToString = "" End If End Function Function MultiByteToBinary(MultiByte) '© 2000 Antonin Foller, http://www.motobit.com ' MultiByteToBinary converts multibyte string To real binary data (VT_UI1 | VT_ARRAY) ' Using recordset Dim RS, LMultiByte, Binary Const adLongVarBinary = 205 Set RS = CreateObject("ADODB.Recordset") LMultiByte = LenB(MultiByte) If LMultiByte>0 Then RS.Fields.Append "mBinary", adLongVarBinary, LMultiByte RS.Open RS.AddNew RS("mBinary").AppendChunk MultiByte & ChrB(0) RS.Update Binary = RS("mBinary").GetChunk(LMultiByte) End If MultiByteToBinary = Binary End Function 
+5
source

Writing to a binary in VBScript is simple, but you need to write one byte at a time. As a demonstration, here is a simple script that creates a single pixel GIF file. The resulting file has exactly binary content written on it, no more, and is a valid GIF file.

 Dim GifFile : Set GifFile = CreateObject("Scripting.FileSystemObject").CreateTextFile("SinglePixel.gif") With GifFile .write chr(&h47) 'GIF87a .write chr(&h49) .write chr(&h46) .write chr(&h38) .write chr(&h37) .write chr(&h61) .write chr(&h01) 'Width .write chr(&h00) .write chr(&h01) 'Height .write chr(&h00) .write chr(&h80) 'Use global color map .write chr(&h00) 'Background .write chr(&h00) 'End of header .write chr(&h00) 'Color map color #1 in RGB .write chr(&h00) .write chr(&h00) .write chr(&hFF) 'Color map color #2 in RGB .write chr(&hFF) .write chr(&hFF) .write chr(&h2C) 'Image descriptor .write chr(&h00) 'Left .write chr(&h00) .write chr(&h00) 'Top .write chr(&h00) .write chr(&h01) 'Width .write chr(&h00) .write chr(&h01) 'Height .write chr(&h00) .write chr(&h40) 'Use global color map / seq order / 1 bit per pixel .write chr(&h02) 'Code size .write chr(&h02) 'Blok byte count .write chr(&h44) 'LZW data .write chr(&h01) .write chr(&h00) 'Terminate data stream .write chr(&h3B) 'Gif terminator End With GifFile.Close 
+1
source

Source: https://habr.com/ru/post/1347157/


All Articles