10 => 16 Hex16 => 2 'Hlen为16进制字符串应有的长度,包括省略的前导0 Function H2B(ByVal str As String, ByVal Hlen As Integer) Dim sChar As String Dim i As Long For i = Len(str) To 1 Step -1 sChar = Mid(str, i, 1) Select Case LCase(sChar) Case "0" H2B = "0000" & H2B Case "1" H2B = "0001" & H2B Case "2" H2B = "0010" & H2B Case "3" H2B = "0011" & H2B Case "4" H2B = "0100" & H2B Case "5" H2B = "0101" & H2B Case "6" H2B = "0110" & H2B Case "7" H2B = "0111" & H2B Case "8" H2B = "1000" & H2B Case "9" H2B = "1001" & H2B Case "a" H2B = "1010" & H2B Case "b" H2B = "1011" & H2B Case "c" H2B = "1100" & H2B Case "d" H2B = "1101" & H2B Case "e" H2B = "1110" & H2B Case "f" H2B = "1111" & H2B End Select Next Dim l As Integer l = Len(str) While l < Hlen H2B = "0000" & H2B l = l + 1 Wend End Function
Option Explicit Public Function num2() As Double Dim i As Integer Dim x As String Dim str As Double Dim sum As Double Dim m As Integer For i = Len(Form1.Text1.Text) To 1 Step -1 x = Mid(Form1.Text1.Text, i, 1) If Asc(x) > 47 And Asc(x) < 50 Then str = Val(x) * 2 ^ m Else MsgBox ("输入不合法") Form1.Text1.SetFocus Form1.Text1.SelStart = i - 1 Form1.Text1.SelLength = 1 Exit Function End If sum = sum + str m = m + 1 Next i num2 = sum End FunctionPublic Function huan8(ten As Double) As String Dim eight As String Dim n As Integer Do Until ten = 0 n = ten Mod 8 eight = n & eight ten = ten \ 8 Loop huan8 = eight End FunctionPublic Function huan16(ten As Double) As String Dim sixteen As String Dim n As Integer Dim x As String Do Until ten = 0 n = ten Mod 16 Select Case n Case 10 x = "A" Case 11 x = "B" Case 12 x = "C" Case 13 x = "D" Case 14 x = "E" Case 15 x = "F" Case Else x = n End Select sixteen = x & sixteen ten = ten \ 16 Loop huan16 = sixteen End FunctionPublic Function num8() As Double Dim i As Integer Dim x As String Dim str As Long Dim sum As Long Dim m As Integer For i = Len(Form1.Text2.Text) To 1 Step -1 x = Mid(Form1.Text2.Text, i, 1) If Asc(x) > 47 And Asc(x) < 56 Then str = Val(x) * 8 ^ m Else MsgBox ("输入不合法") Form1.Text2.SetFocus Form1.Text2.SelStart = i - 1 Form1.Text2.SelLength = 1 Exit Function End If sum = sum + str m = m + 1 Next i num8 = sum End FunctionPublic Function num16() As Double Dim i As Integer Dim x As String Dim str As Double Dim sum As Double Dim m As Integer
For i = Len(Form1.Text3.Text) To 1 Step -1 x = Mid(Form1.Text3.Text, i, 1) If (Asc(x) > 47 And Asc(x) < 58) Or (Asc(x) > 64 And Asc(x) < 91) Then Select Case x Case "A" x = 10 Case "B" x = 11 Case "C" x = 12 Case "D" x = 13 Case "E" x = 14 Case "F" x = 15 Case Else x = x End Select str = Val(x) * 16 ^ m Else MsgBox ("输入不合法") Form1.Text3.SetFocus Form1.Text3.SelStart = i - 1 Form1.Text3.SelLength = 1 Exit Function End If sum = sum + str m = m + 1 Next i num16 = sum End FunctionPublic Function huan2(ten As Double) As String Dim two As String Dim n As Double Do Until ten = 0 n = ten Mod 2 two = n & two ten = ten \ 2 Loop huan2 = two End Function
'Hlen为16进制字符串应有的长度,包括省略的前导0
Function H2B(ByVal str As String, ByVal Hlen As Integer)
Dim sChar As String
Dim i As Long
For i = Len(str) To 1 Step -1
sChar = Mid(str, i, 1)
Select Case LCase(sChar)
Case "0"
H2B = "0000" & H2B
Case "1"
H2B = "0001" & H2B
Case "2"
H2B = "0010" & H2B
Case "3"
H2B = "0011" & H2B
Case "4"
H2B = "0100" & H2B
Case "5"
H2B = "0101" & H2B
Case "6"
H2B = "0110" & H2B
Case "7"
H2B = "0111" & H2B
Case "8"
H2B = "1000" & H2B
Case "9"
H2B = "1001" & H2B
Case "a"
H2B = "1010" & H2B
Case "b"
H2B = "1011" & H2B
Case "c"
H2B = "1100" & H2B
Case "d"
H2B = "1101" & H2B
Case "e"
H2B = "1110" & H2B
Case "f"
H2B = "1111" & H2B
End Select
Next
Dim l As Integer
l = Len(str)
While l < Hlen
H2B = "0000" & H2B
l = l + 1
Wend
End Function
Public Function num2() As Double
Dim i As Integer
Dim x As String
Dim str As Double
Dim sum As Double
Dim m As Integer
For i = Len(Form1.Text1.Text) To 1 Step -1
x = Mid(Form1.Text1.Text, i, 1)
If Asc(x) > 47 And Asc(x) < 50 Then
str = Val(x) * 2 ^ m
Else
MsgBox ("输入不合法")
Form1.Text1.SetFocus
Form1.Text1.SelStart = i - 1
Form1.Text1.SelLength = 1
Exit Function
End If
sum = sum + str
m = m + 1
Next i
num2 = sum
End FunctionPublic Function huan8(ten As Double) As String
Dim eight As String
Dim n As Integer
Do Until ten = 0
n = ten Mod 8
eight = n & eight
ten = ten \ 8
Loop
huan8 = eight
End FunctionPublic Function huan16(ten As Double) As String
Dim sixteen As String
Dim n As Integer
Dim x As String
Do Until ten = 0
n = ten Mod 16
Select Case n
Case 10
x = "A"
Case 11
x = "B"
Case 12
x = "C"
Case 13
x = "D"
Case 14
x = "E"
Case 15
x = "F"
Case Else
x = n
End Select
sixteen = x & sixteen
ten = ten \ 16
Loop
huan16 = sixteen
End FunctionPublic Function num8() As Double
Dim i As Integer
Dim x As String
Dim str As Long
Dim sum As Long
Dim m As Integer
For i = Len(Form1.Text2.Text) To 1 Step -1
x = Mid(Form1.Text2.Text, i, 1)
If Asc(x) > 47 And Asc(x) < 56 Then
str = Val(x) * 8 ^ m
Else
MsgBox ("输入不合法")
Form1.Text2.SetFocus
Form1.Text2.SelStart = i - 1
Form1.Text2.SelLength = 1
Exit Function
End If
sum = sum + str
m = m + 1
Next i
num8 = sum
End FunctionPublic Function num16() As Double
Dim i As Integer
Dim x As String
Dim str As Double
Dim sum As Double
Dim m As Integer
For i = Len(Form1.Text3.Text) To 1 Step -1
x = Mid(Form1.Text3.Text, i, 1)
If (Asc(x) > 47 And Asc(x) < 58) Or (Asc(x) > 64 And Asc(x) < 91) Then
Select Case x
Case "A"
x = 10
Case "B"
x = 11
Case "C"
x = 12
Case "D"
x = 13
Case "E"
x = 14
Case "F"
x = 15
Case Else
x = x
End Select
str = Val(x) * 16 ^ m
Else
MsgBox ("输入不合法")
Form1.Text3.SetFocus
Form1.Text3.SelStart = i - 1
Form1.Text3.SelLength = 1
Exit Function
End If
sum = sum + str
m = m + 1
Next i
num16 = sum
End FunctionPublic Function huan2(ten As Double) As String
Dim two As String
Dim n As Double
Do Until ten = 0
n = ten Mod 2
two = n & two
ten = ten \ 2
Loop
huan2 = two
End Function
1、把待转换的进制数值,转换为十进制
2、把十进制转换为目标进制1:从高位开始,逐位乘幂,如:16进制->[98E]=9*(3-1)+8*(2-1)+E (E为14)
再如:26进制->[XAY]=X*(3-1)+A*(2-1)+Y
-> =23*2+0*1+24
-> =47
2:除以幂取余,倒排列,如:将[XAY]->47 转换为16进制:47/16=2 余 15->F ----> F2
1、把待转换的进制数值,转换为十进制
2、把十进制转换为目标进制1:从高位开始,逐位乘幂,如:16进制->[98E]=9*16^(3-1)+8*16^(2-1)+E*16^0 (E为14)
再如:26进制->[XAY]=X*26^(3-1)+A*26^(2-1)+Y*26^0
-> =23*26^2+0*26^1+24*26^0
-> =15572
2:除以幂取余,倒排列,如:将47 转换为16进制:47/16=2 余 15->F ----> F2