我通过OPC通讯接收到一组字节数组,我想把他变成double型和single型的数据来进行处理,该如何来实现,我知道vb里可以用
for i = 0 to 10
CopyMemory a(i), b(i * 4), 4
next i
的方式来把字节数组b变成single型a,但wincc的vbs脚本里好像没有CopyMemory函数
最佳答案
示例代码:
Function mHexToDecimal(varHexArray)
mHexToDecimal = varHexArray(0)*2^24+varHexArray(1)*2^16+varHexArray(2)*2^8+varHexArray(3)
End Function
Function mHexToBin(varHexArray)
Dim lngDec
Dim strHexToBin
lngDec = CLng(mHexToDecimal(varHexArray))
Do While lngDec > 0
strHexToBin = CStr(lngDec Mod 2) & Trim(strHexToBin)
lngDec = lngDec \ 2
Loop
mHexToBin = strHexToBin
End Function
Function mHexToSingle(varHexArray)
Dim i
Dim BinLen
Dim SngBin
Dim Sign
Dim Exp
Dim Frac
If varHexArray(0) > &H7F Then
Sign = -1
varHexArray(0)=varHexArray(0)-&H80
Else
Sign = +1
End If
SngBin=mHexToBin(varHexArray)
BinLen=Len(SngBin)
For i=1 To 32-BinLen
SngBin="0" & SngBin
Next
For i=1 To 8
Exp=Mid(SngBin, i+1, 1)*2^(8-i)+Exp
Next
Exp=2^(Exp-127)
For i=1 To 23
Frac=Mid(SngBin, i+9, 1)*2^(-i)+Frac
Next
Frac=1+Frac
mHexToSingle=Sign*Exp*Frac
End Function
提问者对于答案的评价:
谢谢,我想问一下double型的话是不是把for循环分别改成1 to 64-BinLen,1 to 8,1 to 56就可以了
最佳答案作者回复:
是的,但mHexToBin,mHexToDecimal可能要有改动,你可以自己试试
原创文章,作者:more0621,如若转载,请注明出处:https://www.zhaoplc.com/plc316903.html