《VB进制转换器.docx》由会员分享,可在线阅读,更多相关《VB进制转换器.docx(5页珍藏版)》请在三一办公上搜索。
1、VB进制转换器VB-进制转换器 一、界面设计 1 / 10 二、代码设计 Private Sub Combo1_Click Text1.SetFocus End Sub Private Sub Combo2_Click Dim num As String num = Text1 If Combo2.Text = 二进制 Then If Combo1.Text = 二进制 Then Text2 = Text1 ElseIf Combo1.Text = 十进制 Then Call DEC_to_BIN(num) ElseIf Combo1.Text = 十六进制 Then Call HEX_to_
2、BIN(num) End If ElseIf Combo2.Text = 十进制 Then If Combo1.Text = 二进制 Then Call BIN_to_DEC(num) ElseIf Combo1.Text = 十进制 Then Text2 = Text1 2 / 10 ElseIf Combo1.Text = 十六进制 Then Call HEX_to_DEC(num) End If ElseIf Combo2.Text = 十六进制 Then If Combo1.Text = 二进制 Then Call BIN_to_HEX(num) ElseIf Combo1.Text
3、= 十进制 Then Call DEC_to_HEX(num) ElseIf Combo1.Text = 十六进制 Then Text2 = Text1 End If End If End Sub Private Sub Command1_Click(Index As Integer) Select Case Index Case 0 Text1 = Text2 = Text1.SetFocus Case 1 End 3 / 10 End Select End Sub Private Sub Form_Resize Form1.Height = 5000 Form1.Width = 8000
4、Form1.Top = 3000 Form1.Left = 4000 Label1.Left = (Form1.Width - Label1.Width) / 2 Combo1.ListIndex = 1 Combo2.ListIndex = 1 End Sub Private Sub DEC_to_BIN(ByVal Dec As Long) 制 tnum = Do While Dec 0 tnum = Dec Mod 2 & tnum Dec = Dec 2 Loop Text2 = tnum End Sub 4 / 10 十进制转换二进Private Sub BIN_to_DEC(ByV
5、al Bin As String) 二进制转换十进制 Dim i As Long, tnum As Long For i = 1 To Len(Bin) tnum = tnum * 2 + Val(Mid(Bin, i, 1) Next i Text2 = tnum End Sub Private Sub DEC_to_HEX(ByVal Dec As Long) 进制 Dim a As String tnum = Do While Dec 0 a = CStr(Dec Mod 16) Select Case a Case 10: a = A Case 11: a = B Case 12: a
6、 = C Case 13: a = D Case 14: a = E Case 15: a = F End Select 5 / 10 十进制转换十六 tnum = a & tnum Dec = Dec 16 Loop Text2 = tnum End Sub Private Sub HEX_to_DEC(ByVal Hex As String) 十六进制转换十进制 Dim i As Long Dim B As Long Hex = UCase(Hex) For i = 1 To Len(Hex) Select Case Mid(Hex, Len(Hex) - i + 1, 1) Case 0
7、: B = B + 16 (i - 1) * 0 Case 1: B = B + 16 (i - 1) * 1 Case 2: B = B + 16 (i - 1) * 2 Case 3: B = B + 16 (i - 1) * 3 Case 4: B = B + 16 (i - 1) * 4 Case 5: B = B + 16 (i - 1) * 5 Case 6: B = B + 16 (i - 1) * 6 Case 7: B = B + 16 (i - 1) * 7 Case 8: B = B + 16 (i - 1) * 8 6 / 10 Case 9: B = B + 16 (
8、i - 1) * 9 Case A: B = B + 16 (i - 1) * 10 Case B: B = B + 16 (i - 1) * 11 Case C: B = B + 16 (i - 1) * 12 Case D: B = B + 16 (i - 1) * 13 Case E: B = B + 16 (i - 1) * 14 Case F: B = B + 16 (i - 1) * 15 End Select Next i Text2 = B End Sub Private Sub BIN_to_HEX(ByVal Bin As String) 二进制转换十六进制 Dim i A
9、s Long Dim H As String If Len(Bin) Mod 4 0 Then Bin = String(4 - Len(Bin) Mod 4, 0) & Bin End If For i = 1 To Len(Bin) Step 4 Select Case Mid(Bin, i, 4) Case 0000: H = H & 0 7 / 10 Case 0001: H = H & 1 Case 0010: H = H & 2 Case 0011: H = H & 3 Case 0100: H = H & 4 Case 0101: H = H & 5 Case 0110: H =
10、 H & 6 Case 0111: H = H & 7 Case 1000: H = H & 8 Case 1001: H = H & 9 Case 1010: H = H & A Case 1011: H = H & B Case 1100: H = H & C Case 1101: H = H & D Case 1110: H = H & E Case 1111: H = H & F End Select Next i While Left(H, 1) = 0 H = Right(H, Len(H) - 1) Wend Text2 = H End Sub 8 / 10 Public Fun
11、ction HEX_to_BIN(ByVal Hex As String) As String 十六进制转换二进制 Dim i As Long Dim B As String Hex = UCase(Hex) For i = 1 To Len(Hex) Select Case Mid(Hex, i, 1) Case 0: B = B & 0000 Case 1: B = B & 0001 Case 2: B = B & 0010 Case 3: B = B & 0011 Case 4: B = B & 0100 Case 5: B = B & 0101 Case 6: B = B & 0110
12、 Case 7: B = B & 0111 Case 8: B = B & 1000 Case 9: B = B & 1001 Case A: B = B & 1010 Case B: B = B & 1011 Case C: B = B & 1100 Case D: B = B & 1101 Case E: B = B & 1110 9 / 10 Case F: B = B & 1111 End Select Next i While Left(B, 1) = 0 B = Right(B, Len(B) - 1) Wend Text2 = B End Function Private Sub Text1_Change a = Len(Text1) If a 0 Then m = Mid(Text1, a, 1) If Combo1.Text = 二进制 And m = 2 Then MsgBox (输入错误) Text1 = Mid(Text1, 1, Len(Text1.Text) - 1) Text1.SelStart = Len(Text1) End If End If End Sub 10 / 10