| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 761 人关注过本帖
标题:简单进制转换(赋源码)
只看楼主 加入收藏
purana
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:广东-广州
等 级:版主
威 望:66
帖 子:6039
专家分:0
注 册:2005-6-17
收藏
 问题点数:0 回复次数:5 
简单进制转换(赋源码)

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'编 写 员 : 梁 嘉 辉
'最 后 更 新 : 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

搜索更多相关主题的帖子: 进制 源码 purana target 
2006-03-31 19:50
kfp_kaka
Rank: 4
等 级:贵宾
威 望:13
帖 子:353
专家分:0
注 册:2006-1-12
收藏
得分:0 
这段代码是.Net的吧?
不错

爱一个人不一定要拥有她 拥有一个人就一定要好好的爱她
2006-04-01 09:21
nobodycan
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2007-7-26
收藏
得分:0 
很好
2007-07-29 19:28
Joforn
Rank: 6Rank: 6
等 级:贵宾
威 望:23
帖 子:1242
专家分:122
注 册:2007-1-2
收藏
得分:0 

用表对换?


VB QQ群:47715789
2007-07-29 20:54
multiple1902
Rank: 8Rank: 8
等 级:贵宾
威 望:42
帖 子:4881
专家分:671
注 册:2007-2-9
收藏
得分:0 
why not use STRINGBUILDER obj?
2007-07-29 22:01
yaoyusheng
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2007-7-21
收藏
得分:0 
太复杂了.例如二进制转换十进制:
Private sub form_click()
a$=inputbox"a="
print "(";a;")2=";
if left$(a,1)="-" then print "-";
m=len(a$)
sum=0
for i=1 to m
b=val(mid$(a$,i,1))
sum=sum+b*2^m-i
next i
print "(";sum;")10"
end sub
2007-07-30 11:41
快速回复:简单进制转换(赋源码)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018004 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved