用0~9和A~Z實現36進制

   一個36進制的函數,是在製作一個軟件需要時做的,使用10個數字和26個字母組成。
過幾天再把進制轉數字的代碼也貼上來。歡迎各位提意見,以求改進!!

    ''' <summary>
    ''' 36進制數加1,0~9,A~Z
    ''' </summary>
    ''' <param name="value"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Str36Add(ByVal value As ObjectOptional ByVal RetBitNum As Integer = 0As 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 StringByVal bitnum As IntegerByVal pstr As StringAs 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 StringAs 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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章