如何在 VBScript 中将二进制数据写入磁盘?

问题描述 投票:0回答:5

我有一个二进制字符串需要写入文件。我有一种感觉,这个应该是一个简单的程序,但话又说回来,VBScript。

FileSystemObject
没有帮助,因为它会破坏数据。
Stream
对象看起来很有前途,它是
adBinaryMode
和它的
Write
方法,但是
Write
方法需要一个字节数组并且似乎不会接受变体数组。由于 VBScript 数组是 all 变体数组,这似乎有问题。

那么,我如何将数据写入文件?

编辑:我应该补充一点,整个事情必须是 VBScript。没有额外的组件。对不起,我也不喜欢。

vbscript asp-classic binary
5个回答
9
投票

普通的也可以

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

2
投票

这里有几个选项。那里描述的最有趣的变体借助自定义函数 BinaryToString.

将二进制数据转换为字符串

2
投票

在 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

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

0
投票

这里是一个使用

System.IO.MemoryStream
获取
ADODB.Stream
所需的“字节”的例子。

Function ArrToBytes(byteArr)
    dim mem,i,v
    Set mem = Server.CreateObject("System.IO.MemoryStream")
    mem.SetLength 0
    For Each v in byteArr 
        mem.WriteByte v
    Next
    ArrToBytes = mem.ToArray()
    Set mem = Nothing
End Function

dim stm
Set stm = Server.CreateObject("ADODB.Stream")
With stm
    .Type = 1
    .Open
    .Write ArrToBytes(Array(&H80, &H81))
    .Write ArrToBytes(Array(&H82, &H83, &H84, &H85))
    .SaveToFile Server.MapPath("test.bin"), 2
    .Close
End With
Set stm = Nothing

灵感来自this answer.

© www.soinside.com 2019 - 2024. All rights reserved.