关于浮点数转为4字节16进制字符串的问题
本帖最后由 bcrun 于 2012-11-18 13:05:07 编辑
在写脚本的时候,需要有代码自动将浮点数(single)转换成(dword)
比如浮点数:0.5 对应的4字节16进制字符串(dword)是: 3F000000
小弟在网上搜索到一段自动转换的脚本,如下文所示。但是不知道如何在VB6里面测试这段代码是否成功。
下文中,我把a = Val(Text1)
改为
Text1="0.5"
a = Val(Text1)
把Text2 = hexData
改为
Text2 = hexData
Print Text2
然后按F5 启动编码,但是在窗体上并没有显示出 Text2 的值(即3F000000)
不知道问题出在哪里?
请大大们指点,谢谢!
=================以下是代码========================
====原文网址====http://blog.csdn.net/zdingyun/article/details/2528026
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Sub BinToSin_Click()
Dim sinStr As String
Dim sinSj As Single
Dim Buffer(3) As Byte
Dim i As Integer
sinStr = Text2
For i = 1 To Len(Text2) Step 2
Buffer((7 - i) / 2) = Val("&H" & Mid(sinStr, i, 2))
Next
CopyMemory ByVal VarPtr(sinSj), ByVal VarPtr(Buffer(0)), 4
Text3 = sinSj
End Sub
Private Sub SinToBin_Click()
Dim i As Integer
Dim hexData As String
Dim a As Single
Dim Buffer(3) As Byte
a = Val(Text1)
CopyMemory Buffer(0), a, 4
For i = 0 To 3
If Len(Hex(Buffer(i))) = 1 Then
hexData = "0" & Hex(Buffer(i)) + hexData
Else
hexData = Hex(Buffer(i)) + hexData
End If
Next
Text2 = hexData
End Sub
Private Sub Form_Load()
Me.AutoRedraw = True
End Sub