vb将单精度转换为4个字符串.txt
Option Explicit
Private Declare Sub CopyMemory Lib \"kernel32\" Alias \"RtlMoveMemory\" (Destination As Any, Source As Any, ByVal Length As Long)
Private Sub cmdSinHex_Click()
Text3 = \"\"
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
For i = 1 To Len(hexData) Step 2
Text3 = Text3 & ChrW(Val(\"&H\" & Mid(hexData, i, 2))) '& \" \"
Next
End Sub
Private Sub Form_Load()
Text1 = \"\"
Text2 = \"\"
Text3 = \"\"
End Sub
VB串口通信中经常会遇到10进制浮点数转为多字节Byte数据类型的情况,以及在接收后需转为10进制浮点数需求。
VB有专门的API函数CopyMemory能处理2-10进制浮点数转换和10-2进制浮点数转换。
下列代码演示了10进制Single(单精度浮点型转为16进制字符显示的浮点数和其相反运算:
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
下列代码演示了10进制Double(双精度浮点型)转为16进制字符显示的浮点数和其相反运算:
Option Explicit
Private Declare Sub CopyMemory Lib \"kernel32\" Alias \"RtlMoveMemory\" (Destination As Any, Source As Any, ByVal Length As Long)
Private Sub cmdDoubHex_Click()
Dim i As Integer
Dim hexData As String
Dim a As Double
Dim Buffer(7) As Byte
a = Val(Text1)
CopyMemory Buffer(0), a, 8
For i = 0 To 7
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 cmdHexDec_Click()
Dim sinStr As String
Dim sinSj As Double
Dim bytes(7) As Byte
Dim i As Integer
sinStr = Text2
For i = 1 To Len(Text2) Step 2
bytes((15 - i) / 2) = Val(\"&H\" & Mid(sinStr, i, 2))
Next
CopyMemory ByVal VarPtr(sinSj), ByVal VarPtr(bytes(0)), 8
Text3 = sinSj
End Sub
但从中无法了解它是如何进行运算处理的。以下通过对Single(单精度浮点型)和Double(双精度浮点型)在内存的储存方式进行分析。
VB的Single 数据类型
Single(单精度浮点型)变量存储为 IEEE 32 位(4 个字节)浮点数值的形式,它的范围在负数的时候是从 -3.402823E38 到 -1.401298E-45,而在正数的时候是从 1.401298E-45 到 3.402823E38。Single 的类型声明字符为感叹号 (!)。
在内存以32位二进制形式存在:
XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX
第1位为符号位
第2-9位为阶码位
第10-32位为2进制小数尾值
即F2 ^ n * 1. XXXXXXX XXXXXXXX XXXXXXXX
其中
F为正号或负号(首为为0正数,首位为1负数
n为2-9位组成的BYTE数据值
XXXXXXX XXXXXXXX XXXXXXXX为尾数
Double(双精度浮点型)变量存储为 IEEE 64 位(8 个字节)浮点数值的形式,它的范围在负数的时候是从 -1.79769313486232E308 到 -4.94065645841247E-324,而正数的时候是从 4.94065645841247E-324 到 1.79769313486232E308。Double 的类型声明字符是数字符号 (#)。
在内存以64位二进制形式存在:
XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX
XXXXXXXX XXXXXXXX
第1位为符号位
第2-12位为阶码位
第13-64位为2进制小数尾值
即F2 ^ n * 1. XXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX
其中
F为正号或负号(首为为0正数,首位为1负数
n为2-12位组成的BYTE数据值
XXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX为尾数
以下代码是基于前叙述的Single(单精度浮点型)在内存的储存方式进行分析后作出的2-10进制浮点数运算:
Option Explicit
Dim hexData As String
Dim i As Single
Dim bindata As String
Dim zs As String * 8
Dim zssz As String
Dim xs As String * 23
Dim xs_js() As Double
Dim xs_hj As Double
Dim sinData As Single
Dim sHex As String
Dim sBin As String
Dim fh As String
Private Sub Command1_Click()
Dim fh As String
sHex = Text1
HexToBin (sHex)
fh = Mid(bindata, 1, 1) '取符号
zs = Mid(bindata, 2, 8) '取指数阶码
xs = Mid(bindata, 10, 23) '取2进制小数
xs_hj = 0
zssz = BinToHex(zs)
ReDim xs_js(1 To 23)
For i = 1 To 23
xs_js(i) = Val(Mid(xs, i, 1))
xs_hj = xs_hj + xs_js(i) / (2 ^ (i))
Next
If zs <> \"00000000\" Then
Shape1.FillColor = vbGreen
If fh = 0 Then
sinData = 2 ^ (Val(\"&H\" & zssz) - 127) * (1 + xs_hj)
ElseIf fh = 1 Then
sinData = -2 ^ (Val(\"&H\" & zssz) - 127) * (1 + xs_hj)
End If
ElseIf sHex = \"00000000\" Then
sinData = 0
Shape1.FillColor = vbGreen
ElseIf zs = \"00000000\" Then '处理在0到1.175494351E-38及
Shape1.FillColor = vbRed '0到-1.175494351E-38间的浮点数
If fh = 0 Then
sinData = 2 ^ (Val(\"&H\" & zssz) - 126) * xs_hj
ElseIf fh = 1 Then
sinData = -2 ^ (Val(\"&H\" & zssz) - 126) * xs_hj
End If
End If
Text2 = sinData
End Sub
Public Function HexToBin(ByVal sHex As String) As String
Const s1 = \"0000101001101111000\
Dim i As Integer, sBin As String
sHex = UCase(sHex)
For i = 1 To Len(sHex)
sBin = sBin & Mid(s1, InStr(1, s2, Mid(sHex, i, 1)), 4)
Next i
HexToBin = sBin
bindata = sBin
End Function
Public Function BinToHex(ByVal sBin As String) As String
Const s1 = \"0000101001101111000\
Dim i As Integer, sHex As String
sBin = String(3 - (Len(sBin) - 1) Mod 4, \"0\") & sBin
For i = 1 To Len(sBin) Step 4
sHex = sHex & Mid(s2, InStr(1, s1, Mid(sBin, i, 4)), 1)
Next i
BinToHex = sHex
End Function
以下代码是基于前叙述的Double(双精度浮点型)在内存的储存方式进行分析后作出的2-10进制浮点数运算:
Option Explicit
Private Declare Sub CopyMemory Lib \"kernel32\" Alias \"RtlMoveMemory\" (Destination As Any, Source As Any, ByVal Length As Long)
Dim hexData As String
Dim i As Single
Dim bindata As String
Dim zs As String '* 8
Dim zssz As String
Dim xs As String '* 23
Dim xs_js() As Double
Dim xs_hj As Double
Dim sinData As Double
Dim sHex As String
Dim sBin As String
Private Sub Command2_Click()
Dim fh As String
sHex = Text2
HexToBin (sHex)
fh = Mid(bindata, 1, 1)
zs = Mid(bindata, 2, 11) '取指数
xs = Mid(bindata, 13, 52) '取2进制小数
xs_hj = 0
zs = \"0\" & zs
zssz = BinToHex(zs)
ReDim xs_js(1 To 52)
For i = 1 To 52
xs_js(i) = Val(Mid(xs, i, 1))
xs_hj = xs_hj + xs_js(i) / (2 ^ (i))
Next
If zs <> \"000000000000\" Then
Shape1.FillColor = vbGreen
If fh = 0 Then
sinData = 2 ^ (Val(\"&H\" & zssz) - 1023) * (1 + xs_hj)
ElseIf fh = 1 Then
sinData = -2 ^ (Val(\"&H\" & zssz) - 1023) * (1 + xs_hj)
End If
ElseIf sHex = \"00000000\" Then
sinData = 0
Shape1.FillColor = vbGreen
ElseIf zs = \"000000000000\" Then '处理在0到2.2250738585072E-308及
Shape1.FillColor = vbRed '0到-2.2250738585072E-308间的浮点数
If fh = 0 Then
sinData = 2 ^ (Val(\"&H\" & zssz) - 1022) * xs_hj
ElseIf fh = 1 Then
sinData = -2 ^ (Val(\"&H\" & zssz) - 1022) * xs_hj
End If
End If
Text3 = sinData
End Sub
Public Function HexToBin(ByVal sHex As String) As String
Const s1 = \"0000101001101111000\
Dim i As Integer, sBin As String
sHex = UCase(sHex)
For i = 1 To Len(sHex)
sBin = sBin & Mid(s1, InStr(1, s2, Mid(sHex, i, 1)), 4)
Next i
HexToBin = sBin
bindata = sBin
End Function
Public Function BinToHex(ByVal sBin As String) As String
Const s1 = \"0000101001101111000\
Dim i As Integer, sHex As String
sBin = String(3 - (Len(sBin) - 1) Mod 4, \"0\") & sBin
For i = 1 To Len(sBin) Step 4
sHex = sHex & Mid(s2, InStr(1, s1, Mid(sBin, i, 4)), 1)
Next i
BinToHex = sHex
End Function
字符串类型转化为单精度类型
Private Sub Command1_Click()
s = \"123.456\"
d = CSng(s)
Print d
End Sub
本来浮点数是这样转化的,但是好象显示的结果和你的有点不同,我不知道什么原因
VB code
Private Declare Sub CopyMemory Lib \"kernel32\" Alias \"RtlMoveMemory\" (Destination As Any, Source As Any, ByVal Length As Long)
Public Function SngToLng(ByVal Value As Single) As Long
CopyMemory SngToLng, Value, 4
End Function
Public Function LngToSng(ByVal Value As Long) As Single
CopyMemory LngToSng, Value, 4
End Function
演讲稿
尊敬的老师们,同学们下午好:
我是来自10级经济学(2)班的学习委,我叫张盼盼,很荣幸有这次机会和大家一起交流担任学习委员这一职务的经验。
转眼间大学生活已经过了一年多,在这一年多的时间里,我一直担任着学习委员这一职务。回望这一年多,自己走过的路,留下的或深或浅的足迹,不仅充满了欢愉,也充满了淡淡的苦涩。一年多的工作,让我学到了很多很多,下面将自己的工作经验和大家一起分享。
学习委员是班上的一个重要职位,在我当初当上它的时候,我就在想一定不要辜负老师及同学们我的信任和支持,一定要把工作做好。要认真负责,态度踏实,要有一定的组织,领导,执行能力,并且做事情要公平,公正,公开,积极落实学校学院的具体工作。作为一名合格的学习委员,要收集学生对老师的意见和老师的教学动态。在很多情况下,老师无法和那么多学生直接打交道,很多老师也无暇顾及那么多的学生,特别是大家刚进入大学,很多人一时还不适应老师的教学模式。学习委员是老师与学生之间沟通的一个桥梁,学习委员要及时地向老师提出同学们的建议和疑问,熟悉老师对学生的基本要求。再次,学习委员在学习上要做好模范带头作用,要有优异的成绩,当同学们向我提出问题时,基本上给同学一个正确的回复。
总之,在一学年的工作之中,我懂得如何落实各项工作,如何和班委有效地分工合作,如何和同学沟通交流并且提高大家的学习积极性。当然,我的工作还存在着很多不足之处。比日:有的时候得不到同学们的响应,同学们不积极主动支持我的工作;在收集同学们对自己工作意见方面做得不够,有些事情做错了,没有周围同学的提醒,自己也没有发觉等等。
最严重的一次是,我没有把英语四六级报名的时间,地点通知到位,导致我们班有4名同学错过报名的时间。这次事使我懂得了做事要脚踏实地,不能马虎。
在这次的交流会中,我希望大家可以从中吸取一些好的经验,带动本班级的学习风气,同时也相信大家在大学毕业后找到好的工作。谢谢大家!
因篇幅问题不能全部显示,请点此查看更多更全内容