工作需要,要用VB写一个接口程序,其中要把浮点型转成Byte数组,用到了一个API,先记录下来,以后C#中可能会用到同样的功能。
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal Destination As Long, ByVal Source As Long, ByVal Length As Long)
浮点转换Byte函数
Private Function FloatToBytes(value As Single) As Byte()
Dim returnByte(4) As Byte
Dim sPtr As Long, bPtr As Long
sPtr = VarPtr(value)
bPtr = VarPtr(returnByte(1))
CopyMemory bPtr, sPtr, 4
FloatToBytes = returnByte
End Function
Byte转换浮点函数
Private Function BytesToFloat(bytes() As Byte) As Single
Dim returnValue As Single
Dim sPtr As Long, bPtr As Long
sPtr = VarPtr(returnValue)
bPtr = VarPtr(bytes(1))
CopyMemory sPtr, ByVal bPtr, 4
BytesToFloat = returnValue
End Function
2008年8月17日星期日
订阅:
博文评论 (Atom)
没有评论:
发表评论