在VB6中我正在使用
Private Type udt1
i As Long
End Type
Private Type udt2
f As Single
End Type
Private Function IntBitsToFloat(ByVal u As Long) As Double
Dim n1 As udt1
n1.i = u
Dim n2 As udt2
LSet n2 = n1
IntBitsToFloat = n2.f
End Function
我试图在VB.NET中找到相同的东西,但我找不到任何东西。
有人知道吗? 谢谢。
答案 0 :(得分:0)
我认为这个可行,基于Wikipedia的一些测试值:
<StructLayout(LayoutKind.Explicit)>
Structure udt
<FieldOffset(0)> Dim i As Integer
<FieldOffset(0)> Dim f As Single
End Structure
Function IntBitsToFloat(ByVal u As Integer) As Single
Dim x As udt
x.i = u
Return x.f
End Function
修改强>
猜猜我应该先搜索一下!请参阅this answer了解一个不那么hacky的版本,在VB.NET中将是:
Function IntBitsToFloat(ByVal u As Integer) As Single
Dim bytes = BitConverter.GetBytes(u)
Return BitConverter.ToSingle(bytes, 0)
End Function
<强>更新强>
在我的电脑上看起来像Structure
的黑客明显更快 - 例如LINQPad中超过100,000,000次迭代:
#1: 00:00:00.0553656
#2: 00:00:01.2785214