[讨论]大家讨论一下这个程序
Function hextobin(hexstr As String) As String ' 由 十六进制串 得到 二进制串
Dim strbin As String
For i = 1 To Len(hexstr)
Select Case UCase(Mid(hexstr, i, 1))
Case "0"
strbin = strbin & "0000"
Case "1"
strbin = strbin & "0001"
Case "2"
strbin = strbin & "0010"
Case "3"
strbin = strbin & "0011"
Case "4"
strbin = strbin & "0100"
Case "5"
strbin = strbin & "0101"
Case "6"
strbin = strbin & "0110"
Case "7"
strbin = strbin & "0111"
Case "8"
strbin = strbin & "1000"
Case "9"
strbin = strbin & "1001"
Case "A"
strbin = strbin & "1010"
Case "B"
strbin = strbin & "1011"
Case "C"
strbin = strbin & "1100"
Case "D"
strbin = strbin & "1101"
Case "E"
strbin = strbin & "1110"
Case "F"
strbin = strbin & "1111"
Case Else
MsgBox "输入的数据有误!!!"
hextobin = "0"
Exit Function
End Select
Next i
hextobin = strbin
End Function
Private Sub Command1_Click()
Dim strbin As String ' 二进制串
Dim stroct As String ' 八进制串
Dim strhex As String ' 十六进制串
Dim strdec As String '十进制串
Dim i As Integer, temp As Long, strtemp As String, numtype As String
numtype = sel.Text
strbin = Trim(num.Text)
deal_line:
Select Case numtype
Case "十进制"
temp = Val(num.Text) ' 直接得到十进制串
For i = 1 To Len(temp)
strtemp = Mid(CStr(temp), i, 1)
If strtemp > "9" Or strtemp < "0" Then
MsgBox "不是十进制数据!!!输入有误!!!"
Exit Sub
End If
Next i
Case "二进制"
temp = 0
For i = Len(strbin) To 1 Step -1
strtemp = Mid(strbin, i, 1)
If strtemp = "1" Then
temp = temp + 2 ^ (Len(strbin) - i)
Else
If strtemp <> "0" Then
MsgBox "不是二进制数据!!!输入错误!!!"
Exit Sub
End If
End If
Next i
Case "八进制"
stroct = Trim(num.Text)
temp = 0
For i = Len(stroct) To 1 Step -1
strtemp = Mid(stroct, i, 1)
If strtemp >= "0" And strtemp <= "7" Then
temp = temp + Val(strtemp) * 8 ^ (Len(stroct) - i)
Else
MsgBox "不是八进制数据!!!输入错误!!!"
Exit Sub
End If
Next i
Case "十六进制"
strhex = Trim(num.Text)
strbin = hextobin(strhex)
numtype = "二进制"
GoTo deal_line
End Select
strdec = CStr(temp)
strhex = UCase(Hex(strdec))
stroct = Oct(strdec)
strbin = hextobin(strhex)
Labresult.Caption = "结果如下:" & Chr(10) & Chr(13) & _
String(10, " ") & "二进制: " & strbin & Chr(10) & Chr(13) & _
String(10, " ") & "八进制: " & stroct & Chr(10) & Chr(13) & _
String(10, " ") & "十进制: " & strdec & Chr(10) & Chr(13) & _
String(10, " ") & "十六进制: " & strhex
End Sub
Private Sub Labresult_Click()
End Sub
Private Sub num_Change()
If num.Text = "" Then
num.Text = "0"
End If
End Sub