关于将single型的变量值copymemory到内存后的格式问题?
将single型的变量值copymemory到内存,再一个字节一个字节读出,不知道结果的格式是什么,请教高人
'以下是single输出结果
-----------------------
Byte 1=0
Byte 2=0
Byte 3=2
Byte 4=66
-----------------------
'以下是double输出结果
Byte 1=0
Byte 2=0
Byte 3=0
Byte 4=0
Byte 5=0
Byte 6=64
Byte 7=64
Byte 8=64
-----------------------
'code
Private Declare Sub CopyMemory Lib "kernel32 " Alias "RtlMoveMemory " (ByVal pDst As Any, ByVal pSrc As Any, ByVal ByteLen As Long)
'API memory functions
Private Declare Function GlobalAlloc Lib "kernel32 " (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GlobalFree Lib "kernel32 " (ByVal hMem As Long) As Long
Private Declare Function GlobalLock Lib "kernel32 " (ByVal hMem As Long) As Long
Private Declare Function GlobalUnlock Lib "kernel32 " (ByVal hMem As Long) As Long
'constants for API memory functions
Private Const GMEM_MOVEABLE = &H2
Private Const GMEM_ZEROINIT = &H40
Private Const GHND = (GMEM_MOVEABLE Or GMEM_ZEROINIT)
Dim pHnd As Long
Dim MemPoint As Long
Private Sub cmd_Free_Click()
' GlobalFree pHnd
End Sub
Private Sub Command1_Click()
Dim i As Long
Dim test As Single
Dim test3 As Single
Dim test2 As Long
Dim ReadMem As Byte
Dim WriteMem As Byte
Dim tmpStr As String
pHnd = GlobalAlloc(GHND, 80)
MemPoint = GlobalLock(pHnd)
tmpStr = " "
test = 32.5
test2 = 0
test3 = 0
ReadMem = 0
CopyMemory MemPoint , VarPtr(test), 4
For i = 1 To 4
CopyMemory VarPtr(ReadMem), MemPoint + i - 1, i
tmpStr = Trim(Str(ReadMem))
Debug.Print "Byte " & Trim(Str(i)) & "= "; tmpStr
ReadMem = 0
Next i
CopyMemory VarPtr(test3), MemPoint, 4
GlobalUnlock pHnd
GlobalFree pHnd
End
End Sub
Private Sub Form_Load()
Call Command1_Click
End Sub
[解决办法]
弯子绕大了,实在不懂转这么大一圈是为了什么?下面的代码就是你要结果:
Private Declare Sub CopyMemory Lib "kernel32 " Alias "RtlMoveMemory " (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
Private Sub Command1_Click()
Dim i As Long, b(3) As Byte
Dim test As Single
test = 32.5
CopyMemory b(0), test, 4
For i = 0 To 3
Debug.Print "Byte " & CStr(i + 1) & "= " & b(i)
Next i
End
End Sub
Private Sub Form_Load()
Call Command1_Click
End Sub