这样结构怎么样用C#来写呢
Private Type HexData4
byte1 As Byte
byte2 As Byte
byte3 As Byte
byte4 As Byte
End Type
Private Type RealData
dataR As Single
End Type
Public Function GetReal(B1 As Byte, B2 As Byte, B3 As Byte, B4 As Byte) As Single
On Error GoTo GetReal_Error
Dim HD As HexData4 ' create user defined types for LSet to work on
Dim RD As RealData ' create user defined types for LSet to work on
HD.byte2 = B1
HD.byte1 = B2
HD.byte4 = B3
HD.byte3 = B4
LSet RD = HD
GetReal = RD.dataR
Debug.Print Format(GetReal, "0.00")
Exit Function ' avoid the error handler
GetReal_Error:
Debug.Print "Invalid Real=" & HD.byte1 & " " & HD.byte2 & " " & HD.byte3 & " " & HD.byte4
GetReal = 0
Resume Next
End Function
------解决方案--------------------GetReal = RD.dataR
=>
return RD.dataR;
------解决方案--------------------其实你不需要这么做:
float getReal(byte b1, byte b2, byte b3, byte b4)
{
return BitConverter.ToSingle(new byte[] { b1, b2, b3, b4 }, 0);
}
------解决方案--------------------不知道lset怎么实现的,可以是直接赋值
那么定义结构时支持FieldOffset就行了
[StructLayout(LayoutKind.Explicit)]
public struct IndexStruct {
[FieldOffset(0)]
public byte B1;
[FieldOffset(1)]
public byte B2;
......
[FieldOffset(0)]
public Single dataR;
}
连转换都不需要了