Dim 十六进制字符集() As String
Dim 八进制字符集() As String
Dim 二进制字符集() As String
Function N进制to十进制(n进制字符 As String, 字符集() As String, Optional 区分大小写 As Boolean = False) As Variant
Dim 进制数 As Long
Dim X As Long
Dim TempStr As String
Dim ReturnLong As Long
Dim NowData As Long
Dim RetDate As Long
进制数 = UBound(字符集) - LBound(字符集) + 1
If 区分大小写 = False Then
'如果不区分大小写就统一字符
n进制字符 = UCase(n进制字符)
End If
For X = 1 To Len(n进制字符)
TempStr = Mid(n进制字符, X, 1)
NowData = -1
RetDate = -1
For Y = LBound(字符集) To UBound(字符集)
NowData = NowData + 1
If TempStr = 字符集(Y) Then
RetDate = NowData
Exit For
End If
Next Y
If RetDate <> -1 Then
ReturnLong = ReturnLong + 进制数 ^ (Len(n进制字符) - X) * RetDate
Else
'非法的进制符号
N进制to十进制 = Null
Exit Function
End If
Next X
N进制to十进制 = ReturnLong
End Function
Private Sub Form_Load()
ReDim 十六进制字符集(0 To 15)
ReDim 八进制字符集(0 To 7)
ReDim 二进制字符集(0 To 1)
Dim X As Long
'初始化二进制字符集
For X = 0 To 1
二进制字符集(X) = CStr(X)
Next X
'初始化八进制字符集
For X = 0 To 7
八进制字符集(X) = CStr(X)
Next X
'初始化十六进制字符集
For X = 0 To 9
十六进制字符集(X) = CStr(X)
Next X
For X = 10 To 15
十六进制字符集(X) = Chr(65 + (X - 10))
Next X
'函数使用范例
Msgbox N进制to十进制("FFFF", 十六进制字符集)
Msgbox N进制to十进制("7777", 八进制字符集)
Msgbox N进制to十进制("110011", 二进制字符集)
End Sub
阅读(325) | 评论(0) | 转发(0) |