vb编程5--十进制转二进制

来源:百度知道 编辑:UC知道 时间:2024/05/15 10:15:16
5.编写程序,在文本框Text1中输入一个十进制整数(可以是负数),单击命令按钮Command1,将该整数转换成

二进制数,并放置在文本框Text2中

Private Sub Command1_Click()
Dim tempData As Double
Dim tempStr1 As String

On Error GoTo ErrorMsg

tempData = Abs(Fix(Val(Text1.Text)))

For j = 0 To 31
If j Mod 4 = 0 Then
tempStr1 = tempData Mod 2 & " " & tempStr1
Else
tempStr1 = tempData Mod 2 & tempStr1
End If
tempData = tempData \ 2
Next

If Left((Text1.Text), 1) = "-" Then Text2.Text = "-" & tempStr1 Else Text2.Text = tempStr1
Exit Sub
ErrorMsg:
Text2.Text = "输入数据错误!!!"
End Sub

Private Sub Form_Load()
Text1.Text = ""
Text2.Text = ""
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)

If (KeyAscii <= vbKey9 And KeyAscii >= vbKey0) Or (KeyAscii = 8 Or KeyAscii = 13) Or KeyAscii =