我怎么能写在VBScript中的二进制数据到磁盘? [英] How can I write binary data to disk in VBScript?

查看:195
本文介绍了我怎么能写在VBScript中的二进制数据到磁盘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个二进制字符串,我需要写一个文件。我有一种感觉,这的的是一个简单的过程,但话又说回来,VBScript中。在 FileSystemObject的是没有帮助的,因为它munges的数据。在对象看起来很有希望,与它的 adBinaryMode 方法,但方法需要一个字节数组,并不会似乎接受一个变量数组来代替。由于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 then again, VBScript. The FileSystemObject is of no help, since it munges the data. The Stream object looks promising, with it's adBinaryMode and its Write method, but the Write method requires a byte array and won't seem to accept a variant array instead. Since VBScript arrays are all variant arrays, this seems problematic.

那么,我怎么才将数据写入一个文件?

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

编辑:我要补充一点,整个事情必须是VBScript中。无需额外的组件。对不起,我也不喜欢。

I should add that the whole thing has to be VBScript. No extra components. Sorry, I don't like it either.

推荐答案

它也有可能与普通的 FileSystemObject的,这里code我使用自定义上传脚本,我写了很久以前使用code我在网上找到二进制字符串转换为ASCII:

It's also possible with the ordinary FileSystemObject, here is code I'm using in custom upload script that I wrote long time ago using code I found online that converts 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

这篇关于我怎么能写在VBScript中的二进制数据到磁盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆