''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'编 写 员 : 梁 嘉 辉
'最 后 更 新 : 2006-3-31
'E-Mail : purana@126.com
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'在vb.net下调试成功...
'在vb里修改一下就行了...
'其它进制转换的明天再写...
Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox2.Text = bin2dec(TextBox1.Text)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = ""
TextBox2.Text = ""
Button1.Text = "转换"
TextBox3.Text = ""
TextBox4.Text = ""
Button2.Text = "转换"
TextBox5.Text = ""
TextBox6.Text = ""
Button3.Text = "转换"
TextBox7.Text = ""
TextBox8.Text = ""
Button4.Text = "转换"
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'二进制转换十进制
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Function bin2dec(ByVal InputData As String) As String
Dim LenInputData As Short
Dim Input As String
Dim i As Short
Dim a As String
Dim decdata As Integer
Input = InputData
LenInputData = Len(InputData)
If LenInputData = 0 Then
MessageBox.Show("请输入二进制数!", "输入", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
TextBox1.Focus()
Exit Function
End If
If InStr(InputData, ".") > 0 Then
MessageBox.Show("不能输入小数点", "错误", MessageBoxButtons.OKCancel)
TextBox1.SelectionStart = 0
TextBox1.SelectionLength = Len(TextBox1.Text)
TextBox1.Focus()
Exit Function
End If
For i = 1 To LenInputData '判断是否为合法的二进制数
If Mid(Input, i, 1) <> "0" And Mid(Input, i, 1) <> "1" Then
MessageBox.Show("输入错误,不是二进制数")
TextBox1.SelectionStart = 0
TextBox1.SelectionLength = Len(TextBox1.Text)
TextBox1.Focus()
Exit Function
End If
Next
For i = LenInputData To 1 Step -1 '将二进制转换成十进制数
a = Mid(Input, i, 1)
If a = "1" Then
decdata = decdata + 2 ^ (LenInputData - i)
End If
Next
Return CStr(decdata)
End Function
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox4.Text = bin2hex(TextBox3.Text)
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'二进制转十六进制
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Function bin2hex(ByVal InputData As String) As String
Dim lendata As Integer
Dim DataStr As String
Dim i As Short
Dim sour As String
Dim destdec As String
If InputData = "" Then
MessageBox.Show("请输入内容!", "输入", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
TextBox1.Focus()
Exit Function
End If
If InStr(InputData, ".") > 0 Then
MessageBox.Show("不能输入小数点", "错误", MessageBoxButtons.OKCancel)
TextBox3.SelectionStart = 0
TextBox3.SelectionLength = Len(TextBox3.Text)
TextBox3.Focus()
Exit Function
End If
lendata = Len(InputData)
For i = 1 To lendata '判断是否为合法的二进制数
If Mid(InputData, i, 1) <> "0" And Mid(InputData, i, 1) <> "1" Then
MessageBox.Show("输入错误,不是二进制数")
TextBox3.SelectionStart = 0
TextBox3.SelectionLength = Len(TextBox3.Text)
TextBox3.Focus()
Exit Function
End If
Next
DataStr = InputData
If lendata Mod 4 <> 0 Then
If lendata Mod 4 = 1 Then
DataStr = "000" & InputData
ElseIf lendata Mod 4 = 2 Then
DataStr = "00" & InputData
ElseIf lendata Mod 4 = 3 Then
DataStr = "0" & InputData
End If
End If
For i = 1 To Len(DataStr) Step 4
sour = Mid(DataStr, i, 4)
If sour = "0000" Then
destdec = destdec & "0"
ElseIf sour = "0001" Then
destdec = destdec & "1"
ElseIf sour = "0010" Then
destdec = destdec & "2"
ElseIf sour = "0011" Then
destdec = destdec & "3"
ElseIf sour = "0100" Then
destdec = destdec & "4"
ElseIf sour = "0101" Then
destdec = destdec & "5"
ElseIf sour = "0110" Then
destdec = destdec & "6"
ElseIf sour = "0111" Then
destdec = destdec & "7"
ElseIf sour = "1000" Then
destdec = destdec & "8"
ElseIf sour = "1001" Then
destdec = destdec & "9"
ElseIf sour = "1010" Then
destdec = destdec & "A"
ElseIf sour = "1011" Then
destdec = destdec & "B"
ElseIf sour = "1100" Then
destdec = destdec & "C"
ElseIf sour = "1101" Then
destdec = destdec & "D"
ElseIf sour = "1110" Then
destdec = destdec & "E"
ElseIf sour = "1111" Then
destdec = destdec & "F"
End If
Next
Return destdec
End Function
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
TextBox6.Text = hex2bin(TextBox5.Text)
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'十六进制转换二进制
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Function hex2bin(ByVal InputData As String) As String
Dim i As Short
Dim hexstr As String
Dim a As String
If InputData = "" Then
MessageBox.Show("请输入内容!", "输入", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
TextBox5.Focus()
Exit Function
End If
If InStr(InputData, ".") > 0 Then
MessageBox.Show("不能输入小数点", "错误", MessageBoxButtons.OKCancel)
TextBox5.SelectionStart = 0
TextBox5.SelectionLength = Len(TextBox5.Text)
TextBox5.Focus()
Exit Function
End If
For i = 1 To Len(InputData)
a = Mid(InputData, i, 1)
If Not IsNumeric(a) And a <> "A" And a <> "B" And a <> "C" And a <> "D" And a <> "E" And a <> "F" Then
MessageBox.Show("输入错误,不是十六进制数", "错误", MessageBoxButtons.OKCancel)
TextBox5.SelectionStart = 0
TextBox5.SelectionLength = Len(TextBox5.Text)
TextBox5.Focus()
Exit Function
End If
Next
For i = 1 To Len(InputData)
a = Mid(InputData, i, 1)
If a = "0" Then
hexstr = hexstr & "0000"
ElseIf a = "1" Then
hexstr = hexstr & "0001"
ElseIf a = "2" Then
hexstr = hexstr & "0010"
ElseIf a = "3" Then
hexstr = hexstr & "0011"
ElseIf a = "4" Then
hexstr = hexstr & "0100"
ElseIf a = "5" Then
hexstr = hexstr & "0101"
ElseIf a = "6" Then
hexstr = hexstr & "0110"
ElseIf a = "7" Then
hexstr = hexstr & "0111"
ElseIf a = "8" Then
hexstr = hexstr & "1000"
ElseIf a = "9" Then
hexstr = hexstr & "1001"
ElseIf a = "A" Then
hexstr = hexstr & "1010"
ElseIf a = "B" Then
hexstr = hexstr & "1011"
ElseIf a = "C" Then
hexstr = hexstr & "1100"
ElseIf a = "D" Then
hexstr = hexstr & "1101"
ElseIf a = "E" Then
hexstr = hexstr & "1110"
ElseIf a = "F" Then
hexstr = hexstr & "1111"
End If
Next
Return hexstr
End Function
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'十进制转换十六进制
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click
TextBox8.Text = Hex(TextBox7.Text)
End Sub
End Class