该死的,我怎么可能忘记C风格的联盟呢?
<Runtime.InteropServices.StructLayout(Runtime.InteropServices.LayoutKind.Explicit)> _ Public Structure IntFloatUnion <Runtime.InteropServices.FieldOffset(0)> Public i As Integer <Runtime.InteropServices.FieldOffset(0)> Public f As Single End Structure Sub Main() Dim u As IntFloatUnion u.i = 42 Console.WriteLine(u.f) Console.ReadLine() End Sub
public static class FancyConverter { public static unsafe float FloatFromBytes(int i) { return *((float*)(void*)(&i)); } }
这可以编译为单独的dll并从VB项目中引用.