VB十进制二进制互转(含小数位)审计署计算机中级培训第三周,城厢区审计局陈剑芳
十转换二:
Private Sub Command1_Click()
Dim a As Single, b As Integer, c As Single, d As Integer, e As Integer, x As String, y As String
a = CSng(Text1.Text)
'if a mod 1=0
b = Fix(a)
Do While b > 0
If b Mod 2 = 0 Then
'Print 0
x = "0" & x
b = b \ 2
Else
'Print 1
x = "1" & x
b = b \ 2
End If
Loop
'Else
c = a - Fix(a)
'Print c
For e = 1 To 6 Step 1
If Fix(c * 2) >= 1 Then
'Print 1
y = y & "1"
c = c * 2 - 1
Else
'Print 0
y = y & "0"
c = c * 2
End If
Next
Text2.Text = Trim(x & "." & y)
End Sub
二进转十进:
Private Sub Command2_Click()
Dim a As Integer, b As Integer, c As Integer
Dim s As Integer, s1 As Integer, t As Integer, t1 As Integer, t2 As Integer
Dim q As Double, r As Double, q1 As Double, r1 As Double, a1 As Double, a2 As Double, a3 As Double
Dim i As Integer, flag As Boolean
Dim str1 As String, str2 As String
r = CDbl(Text2.Text)
'Print r
c = Len(Text2.Text)
'Print p
a = 0
s = 1
vba 字符串转数组s1 = 1
a2 = 0
a1 = 0
a4 = 0
flag = False
str1 = Trim(Text2.Text)
For i = 1 To Len(str1)
str2 = Mid(str1, i, 1)
If (str2 = ".") Or (str2 = "0") Or (str2 = "1") Then
Else
flag = True
Exit For
End If
Next
If flag = True Then
MsgBox "输入错误,请重新输入!" Text2.Text = ""
Text1.Text = ""
Else
For b = 1 To Len(Text2.Text) Step 1 If Mid(Text2.Text, b, 1) = "." Then
a = b
End If
Next
If a = 0 Then
For t = c To 1 Step -1
q = Mid(r, s, 1) * 2 ^ (t - 1)
'Print q
a1 = a1 + q
s = s + 1
Next
Text1.Text = CStr(a1)
Else
For t1 = a - 1 To 1 Step -1
q = Mid(r, s, 1) * 2 ^ (t1 - 1)
'Print q
a1 = a1 + q
s = s + 1
Next
For t2 = 1 To c - a Step 1
q1 = Mid(r, s1 + a, 1) * 2 ^ (-s1)
'Print q
a2 = a2 + q1
s1 = s1 + 1
Next
a3 = a1 + a2
Text1.Text = CStr(a3)
End If
'flag = True
End If
End Sub

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。