代码之家  ›  专栏  ›  技术社区  ›  spoulson

如何在VBScript中附加二进制值

  •  0
  • spoulson  · 技术社区  · 17 年前

    如果有两个变量包含二进制值,如何将它们作为一个二进制值附加在一起?例如,如果我使用WMI读取两个REG_二进制值的注册表,那么我希望能够连接这些值。

    3 回复  |  直到 17 年前
        1
  •  2
  •   AnthonyWJones    17 年前

    REG_二进制值将作为字节数组返回。VBScript可以引用变量中的字节数组,它可以将此字节数组作为变量传递给另一个函数或作为对字节数组的引用。但是VBScript本身对数组无能为力。

    您将需要一些其他组件来执行一些连接:-

    Function ConcatByteArrays(ra, rb)
        Dim oStream : Set oStream = CreateObject("ADODB.Stream")
        oStream.Open
        oStream.Type = 1 'Binary'
        oStream.Write ra
        oStream.Write rb
    
        oStream.Position = 0
    
        ConcatByteArrays = oStream.Read(LenB(ra) + LenB(rb))
        oStream.Close
    
    End Function
    

    如果实际有多个要连接的数组,则可以使用以下类:-

    Class ByteArrayBuilder
        Private moStream
    
        Sub Class_Initialize()
            Set moStream = CreateObject("ADODB.Stream")
            moStream.Open
            moStream.Type = 1
        End Sub
    
        Public Sub Append(rabyt)
            moStream.Write rabyt
        End Sub
    
        Public Property Get Length
            Length = moStream.Size
        End Property
    
        Public Function GetArray()
            moStream.Position = 0
            GetArray = moStream.Read(moStream.Size)
        End Function
    
        Sub Class_Terminate()
            moStream.Close
        End Sub
    
    End Class
    

    调用append的次数与使用数组的次数相同,并使用GetArray检索结果数组。

        2
  •  1
  •   spoulson    17 年前

    作为记录,我想要一个大用户群的VBScript代码作为一个失败几率最小的登录脚本。我喜欢ADO对象,但是有很多神秘的方法可以破坏ADO,所以我避开ADODB.Stream。

    REG_BINARY 值,我将其转换为整数数组,并将其交给 SetBinaryValue WMI方法。

    注: WshShell 只能处理 值包含4个字节,因此无法使用。

        3
  •  0
  •   aphoria    17 年前

    可能

    result = CStr(val1) & CStr(val2)