我有一个二进制字符串,我需要写入文件。我觉得这个应该是一个简单的过程,但是再一次,VBScript。 FileSystemObject
没有任何帮助,因为它可以提供数据。 Stream
对象看起来很有希望,它是adBinaryMode
及其Write
方法,但Write
方法需要一个字节数组,而不会接受变量数组。由于VBScript数组是所有变体数组,因此这似乎有问题。
那么,我该如何将数据写入文件?
编辑:我应该补充一点,整个事情必须是VBScript。没有额外的组件。对不起,我也不喜欢它。答案 0 :(得分:5)
也可以使用普通的FileSystemObject
,这里是我在自定义上传脚本中使用的代码,我很久以前使用在线找到的代码将二进制字符串转换为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
答案 1 :(得分:2)
Here有几种选择。其中描述的最有趣的变体是在自定义函数BinaryToString的帮助下将二进制数据转换为字符串。
答案 2 :(得分:1)
在VBScript中写入二进制文件很简单,但要求您一次写入一个字节。作为演示,这是一个创建单个像素GIF文件的简单脚本。生成的文件正好写入了二进制内容,仅此而已,并且是一个有效的GIF文件。
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
答案 3 :(得分:0)
使用FileSystemObject
将(十六进制编码的)二进制字符串写入文件:
Dim File : Set File = CreateObject("Scripting.FileSystemObject").CreateTextFile("Binary.gif")
data = "47 49 46 38 37 61 07 00 07 00 80 F0 00 FF 00 00 00 FF FF 2C 00 00 00 00 07 00 07 00 40 02 02 44 01 00 3B"
data = Split (data)
for each x in data
File.write chr("&H" & x)
next
File.Close