今天写了一个程序需要函数返回数组,看了别的网友的程序发现有bug,特此记录一下
返回函数写法
Private Function Fun_Fp32ToByte(ByVal fp32 As Single) As Byte()
Dim byt(3) As Byte
byt = System.BitConverter.GetBytes(fp32)
Debug.WriteLine("Gao " & byt(3).ToString("X2"))
Debug.WriteLine("ZhongGao " & byt(2).ToString("X2"))
Debug.WriteLine("ZhongDi " & byt(1).ToString("X2"))
Debug.WriteLine("Di " & byt(0).ToString("X2"))
Return byt
End Function
调用方法
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
Dim tmp(3) As Byte
tmp = Fun_Fp32ToByte(Fun_ByteToFp32(&HC2, &H54, &H80, &H0))
For Each td As Byte In tmp
Debug.WriteLine("数组返回 " & td.ToString("X2"))
Next
End Sub
辅助函数
'浮点数转换
Private Function Fun_ByteToFp32(ByVal gao As Byte, ByVal zhonggao As Byte, ByVal zhongdi As Byte, ByVal di As Byte) As Single
Dim byt(4) As Byte
byt(3) = gao
byt(2) = zhonggao
byt(1) = zhongdi
byt(0) = di
Return System.BitConverter.ToSingle(byt, 0)
End Function
输出结果
Gao C2
ZhongGao 54
ZhongDi 80
Di 00
数组返回 00
数组返回 80
数组返回 54
数组返回 C2