一、在全局脚本里面新建两个函数:
代码如下:
'''
'''16个Bit转Word
'''接收参数为包含16个元素的数组
'''其中每个元素依次对应16个Bit中的一个
Function bits2Word(Byval arr)
Dim res
res = 0
If Not IsArray(arr) Then
Msgbox "arr is not a array."
Elseif UBound(arr) <> 15 Then
Msgbox "the length of array must be 16."
Else
Dim x,y,idx
For idx = 0 To 15
x = arr(idx)
y = pow(2,(15 - idx))
res = res + (x * y)
Next
End If
bits2Word = res
End Function
'''辅助函数:求num的n次方
Function pow(Byval num,Byval n)
If n = 0 Then
pow = 1
Else
pow = pow(num , n - 1) * num
End If
End Function
二、在VBS脚本中调用:
代码调用实例如下:
Dim word
word = bits2Word(Array(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1))
Msgbox word

![]()
本文介绍了一个VBScript函数,用于将16位比特数组转换成一个Word数值。该函数首先检查输入是否为16个元素的数组,然后通过遍历数组并计算每位对应的权重来实现转换。
3万+

被折叠的 条评论
为什么被折叠?



