一个36进制的函数,是在制作一个软件需要时做的,使用10个数字和26个字母组成。
过几天再把进制转数字的代码也贴上来。欢迎各位提意见,以求改进!!
''' 36进制数加1,0~9,A~Z
''' </summary>
''' <param name="value"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function Str36Add(ByVal value As Object, Optional ByVal RetBitNum As Integer = 0) As String
If value.GetType.Name <> "String" And Not (value Is DBNull.Value) Then
Str36Add = ""
MsgBox("参数错误,应为字符型数据或空值。")
Exit Function
End If
If value Is DBNull.Value Then
Str36Add = AddBit("1", RetBitNum, "0")
Exit Function
End If
Dim tmp As String = UCase(Trim(value))
Dim i As Integer
Dim newtmp As String = ""
For i = 1 To Len(tmp)
If newtmp <> "" Then
newtmp = newtmp & Mid(tmp, i, 1)
Else
If Mid(tmp, i, 1) <> "0" Then
newtmp = Mid(tmp, i, 1)
End If
End If
Next
tmp = newtmp
If String.IsNullOrEmpty(tmp) Then
Str36Add = AddBit("1", RetBitNum, "0")
Exit Function
End If
If tmp <> "" And Not IsString(tmp) Then
Str36Add = ""
MsgBox("错误,非字符串。", 16, "参数错误")
Exit Function
End If
Dim newstr As String = ""
Dim laststr As String = ""
Str36Add = ""
Dim addstr As String = ""
Dim addvalue As String = ""
For i = Len(tmp) To 1 Step -1
laststr = Mid(tmp, i, 1)
addvalue = straddone(laststr)
'先判断这位数字是否小于字母“Z”,如不小於则返回值
If Asc(laststr) < 90 Then
Str36Add = AddBit(Mid(tmp, 1, i - 1) & addvalue & addstr, RetBitNum, "0")
Exit Function
End If
If i = 1 Then
Str36Add = AddBit("1" & addvalue & addstr, RetBitNum, "0")
Exit Function
Else
addstr = addvalue & addstr
End If
Next
Str36Add = AddBit(Str36Add, RetBitNum, "0")
End Function
Function AddBit(ByVal OldStr As String, ByVal bitnum As Integer, ByVal pstr As String) As String
AddBit = OldStr
If bitnum = 0 Then
Exit Function
End If
Dim tmp As String = Left(pstr, 1)
If tmp = "" Then
tmp = " "
End If
Dim i As Integer
For i = 1 To bitnum - Len(OldStr)
AddBit = tmp & AddBit
Next
End Function
Function straddone(ByVal value As String) As String
If Asc(value) = 57 Then
straddone = "A"
Exit Function
End If
If Asc(value) = 90 Then
straddone = "0"
Exit Function
End If
straddone = Chr(Asc(value) + 1)
End Function