📄 进制转换.frm
字号:
VERSION 5.00
Begin VB.Form Form1
Caption = "2-36进制数随便转换"
ClientHeight = 3330
ClientLeft = 60
ClientTop = 345
ClientWidth = 4980
LinkTopic = "Form1"
ScaleHeight = 3330
ScaleWidth = 4980
StartUpPosition = 3 '窗口缺省
Begin VB.CommandButton cmdConvert
Caption = "转换"
BeginProperty Font
Name = "黑体"
Size = 15
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 615
Left = 1680
TabIndex = 4
Top = 2460
Width = 1215
End
Begin VB.TextBox txtBase2
BeginProperty Font
Name = "黑体"
Size = 15
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 495
Left = 3120
TabIndex = 3
Top = 1680
Width = 495
End
Begin VB.TextBox txtBase1
BeginProperty Font
Name = "黑体"
Size = 15
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 495
Left = 780
TabIndex = 2
Top = 1680
Width = 495
End
Begin VB.TextBox txtFrom
Alignment = 1 'Right Justify
BeginProperty Font
Name = "Arial Black"
Size = 15
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 495
Left = 1680
TabIndex = 0
Top = 180
Width = 2835
End
Begin VB.Label Label6
AutoSize = -1 'True
Caption = "进制"
BeginProperty Font
Name = "黑体"
Size = 15
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 300
Left = 3780
TabIndex = 9
Top = 1800
Width = 600
End
Begin VB.Label Label5
AutoSize = -1 'True
Caption = "进制转换为"
BeginProperty Font
Name = "黑体"
Size = 15
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 300
Left = 1440
TabIndex = 8
Top = 1800
Width = 1500
End
Begin VB.Label Label4
AutoSize = -1 'True
Caption = "从"
BeginProperty Font
Name = "黑体"
Size = 15
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 300
Left = 360
TabIndex = 7
Top = 1800
Width = 300
End
Begin VB.Label lblTo
Alignment = 1 'Right Justify
BackColor = &H8000000E&
BorderStyle = 1 'Fixed Single
BeginProperty Font
Name = "Arial Black"
Size = 15
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 480
Left = 1680
TabIndex = 6
Top = 900
Width = 2850
End
Begin VB.Label Label2
AutoSize = -1 'True
Caption = "转换结果"
BeginProperty Font
Name = "黑体"
Size = 15
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 300
Left = 360
TabIndex = 5
Top = 960
Width = 1200
End
Begin VB.Label Label1
AutoSize = -1 'True
Caption = "欲转换数据"
BeginProperty Font
Name = "黑体"
Size = 15
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 300
Left = 60
TabIndex = 1
Top = 240
Width = 1500
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
' 通用进制转换程序
' 本程序演示了循环的使用和字符处理的技巧。
' 本程序可以支持2-36间的各进制数互转。
' 如果想支持更大的进制(比如说100),请在DS中加入更多的“数字”字符
' 如果没更多的字符可用,可以区分大小写,将程序中的UCASE函数去掉
' 曹新国,编写于2004-3-24
Option Explicit
Private Sub cmdConvert_Click()
Dim B1%, B2%, N&, L%, I%, J%
Dim DS$, DS1$, C$, S$
'在DS中生成数值字符表0123456789ABCDEF...Z
DS = "0123456789"
For I = 0 To 25: DS = DS + Chr(Asc("A") + I): Next I
'数制有效性检查
B1 = txtBase1 '原数制
B2 = txtBase2 '要转换数制
If B1 < 2 Or B1 > 36 Or B2 < 2 Or B2 > 36 Then
MsgBox "指定数制无效,必须界于2-36间"
Exit Sub
End If
'源数据有效性检查,并转换为数值(长整型)
txtFrom = UCase(Trim(txtFrom)) '滤掉空格并转换为大写
L = Len(txtFrom)
N = 0
DS1 = Left(DS, B1) '取出源进制的有效字符,如B1=4,则取出"0123"
For I = 1 To L
C = Mid(txtFrom, I, 1)
J = InStr(DS1, C) '查找单个字符在表中的位置
If J = 0 Then '=0说明不在表中,属无效字符
MsgBox "数据中有非法字符"
Exit Sub
End If
N = N * B1 + J - 1 '第一个是0,第二个是1,所以要减1
Next
'至此,N中已经包括欲转换的数值
S = ""
Do While N >= 1
J = N Mod B2
'取出的时候要加1 ,因为0要取第1个字符,1要取第二个,依此类推
S = Mid(DS, J + 1, 1) + S
N = N \ B2
Loop
lblTo = S
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -