重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
使用API函数CopyMemory来转换。
创新互联公司是专业的丘北网站建设公司,丘北接单;提供成都做网站、成都网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行丘北网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Sub Command1_Click()
Dim BB(0 To 3) As Byte
Dim se As Single
BB(0) = H0
BB(1) = H80
BB(2) = H71
BB(3) = H43
CopyMemory se, ByVal VarPtr(BB(0)), 4
Text1.Text = se
End Sub
具体参考:
四字节16进制数可转换为四个二进制字节(Bytes),从而使用Api函数来处理成单精度浮点数。
以下用VB代码处理。
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
需要注意的事项是这四字节的高低字节排序,不能搞错。
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为尾数。
参考:
#读取上次检查到的行数
last_line=`cat ./mailinfo`
echo "上次行数:"$last_line
#算出本次alert日志的行数
current_line=`wc -l $alert_log|awk '{print $1}'`
echo "本次行数:"$current_line
#取上次行数和本次行数之间的alert日志内容,然后把ORA错误及上下三行放到mailinfo中
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim aa(3) As Byte
Dim f As Single
aa(0) = H0
aa(1) = H0
aa(2) = H80
aa(3) = H3F
'CopyMemory(f, aa, 4)
f = BitConverter.ToSingle(aa, 0)
TextBox1.Text = f
End Sub
End Class
Option Explicit
Dim Jz As Integer, K As Boolean
Private Sub Command1_Click()
Pic.Cls
If K Then Jz = Val(Qtjz.Text)
If Pd(Text1.Text, Jz) Then Pic.Print "选择的进制与数不符": Exit Sub
If Jz = 0 Then Pic.Print "请先输入进制" Else Pic.Print Zh(Text1.Text, Jz)
End Sub
Private Sub Command2_Click()
End
End Sub
Private Sub Frame1_DragDrop(Source As Control, X As Single, Y As Single)
End Sub
Private Sub Option1_Click(Index As Integer)
K = False
Select Case Index
Case 0
Jz = 2
Case 1
Jz = 8
Case 2
Jz = 10
Case 3
Jz = 16
Case 4
K = True
Qtjz.Text = ""
Qtjz.SetFocus
End Select
If Index 4 Then Command1.SetFocus
End Sub
Private Sub Qtjz_Click()
Qtjz.Text = ""
End Sub
Private Sub Text1_Click()
Text1.Text = ""
End Sub
Private Function Zh(a As String, b As Integer) As Integer
Dim c As String, i, d As Integer
a = LCase(a)
For i = 1 To Len(a)
c = Mid(a, Len(a) + 1 - i, 1)
If Asc(c) 96 Then d = Asc(c) - 87 Else d = Val(c)
Zh = Zh + d * b ^ (i - 1)
Next i
End Function
Private Function Pd(a As String, b As Integer) As Boolean
Dim c As String, i, d As Integer
Pd = False
For i = 1 To Len(a)
c = Mid(a, Len(a) + 1 - i, 1)
If Asc(c) 96 Then d = Asc(c) - 87 Else d = Val(c)
If d b - 1 Then Pd = True: Exit Function
Next i
End Function
'//首先,在工程中加入心缘梦恋VB计算类模块“bMaths”'//'//全局定义Private B1 As New bMaths'//在窗体中写入自定义函数:Private Function HexToFloat(ByVal Hexadecimal As String) As Single
Dim tmp_B As String
tmp_B = B1.Bin(Text1(2).Text)
'
Dim Power As Integer, Effective As Long, s_Effective As String, PN As Byte
'//幂(Power);Effective(有效数);PN(正负定义)
Power = Val("H" B1.HexB(Mid(tmp_B, 2, 8))) - 127
s_Effective = Right(tmp_B, 23)
s_Effective = "1" s_Effective
Select Case Power
Case Is 0
Power = 0 - Power
Effective = Val("H" B1.HexB(s_Effective))
PN = Val(Left(tmp_B, 1))
HexToFloat = ((-1) ^ PN) * (Effective / (2 ^ (23 + Power)))
Case Is = 0
Dim s_Front As String, s_Follow As String
s_Front = Left(s_Effective, 1 + Power)
s_Follow = Right(s_Effective, 23 - Power)
PN = Val(Left(tmp_B, 1))
HexToFloat = ((-1) ^ PN) * (Val("H" B1.HexB(s_Front)) + Val("H" B1.HexB(s_Follow)) / (2 ^ (23 - Power)))
End Select
End Function'//'//调用函数HexToFloat,将十六进制数值转化为浮点型数据'//例如,将“43730000”转化为浮点型数据Private Sub Form_Load()
Debug.Print HexToFloat("43730000")
End Sub'//输出结果:“243”'//'//特别提醒:此段函数还没有考虑浮点数据的科学计数法。不过,这段函数在一般情况下已经够用了。如果还有好的建议,您可以加入我们的群(ID:18858430),一起探讨。