<%
' 名称:HDOBTools
' 描述:进制转化类
' 作用:用于各种进转的转化
Class HDOBTools
    Private hdobHack
    Private Sub Class_Initialize()
        Set hdobHack = New HDOBTools_Hack
    End Sub
    Private Sub Class_Terminate()
        Set hdobHack = Nothing
    End Sub

    ' 十进制转二进制
    Function Bit(num)
        Bit = Dec2Other(num,2)
    End Function
    
    ' 十进制转八进制
    ' 此函数其实不必添加,只是为了补全,所以需借助附加类
    Function [Oct](num)
        [Oct] = hdobHack.dec2oct(num)
    End Function

    ' 十进制转十六进制
    ' 此函数其实不必添加,只是为了补全,所以需借助附加类
    Function [Hex](num)
        [Hex] = hdobHack.dec2hex(num)
    End Function

    ' 十进制转三十二进制
    ' 自我扩展的:Str16_31 = "GHIJKLMNOPQRSTUV"
    Function Ext32(num)
        Ext32 = Dec2Other(num,32)
    End Function

    ' 十进制转六十四进制
    ' 自我扩展的:Str16_31 = "GHIJKLMNOPQRSTUV"
    ' 自我扩展的:Str32_63 = "WXYZ+=abcdefghijklmnopqrstuvwxyz"
    Function Ext64(num)
        Ext64 = Dec2Other(num,64)
    End Function

    ' 二进制转十进制
    Function Bit2Dec(num)
        Bit2Dec = Other2Dec(num,2)
    End Function
    
    ' 八进制转十进制
    Function Oct2Dec(num)
        Oct2Dec = Other2Dec(num,8)
    End Function
    
    ' 十六进制转十进制
    Function Hex2Dec(num)
        Hex2Dec = Other2Dec(num,16)
    End Function

    
    ' 各种进制的转化
    Function Other2Other(num,x1,x2)
        If x1=x2 Then Other2Other = num : Exit Function
        If x1 = 10 Then
            Other2Other = Dec2Other(num,x2)
        ElseIf x2 = 10 Then
            Other2Other = Other2Dec(num,x1)    
        Else
            Other2Other = Dec2Other(Other2Dec(num,x1),x2)
        End If
    End Function

    ' 十进制转x(x=2,8,16,32,64)进制
    ' 结果为字符串
    ' 注:对于2,8进制,结果为(全)数字字符串,对于16以上进制,结果不一定为数字字符串
    Function Dec2Other(num,x)
        '对于十进制转8或16进制,有专门的内置函数可用(当然,也可按常规转化)
        If x = 8 Then
            Dec2Other = Oct(num) : Exit Function
        ElseIf x = 16 Then
            Dec2Other = Hex(num) : Exit Function
        End If
        
        'Dim Str16_31 : Str16_31 = "GHIJKLMNOPQRSTUV"    '32位
        'Dim Str32_63 : Str32_63 = "WXYZ+=abcdefghijklmnopqrstuvwxyz"    '64位
        Dim strBit : strBit = Empty
        Dim tmp
        Do 
            tmp = (num Mod x)
            If tmp = 36 Then
                tmp = "+"
            ElseIf tmp = 37 Then
                tmp = "="
            ElseIf tmp > 37 Then    'a(97-->38)
                tmp = Chr(97+tmp-37-1)
            ElseIf tmp > 9 Then 
                tmp = Chr(65+tmp-9-1)
            End If
            
            strBit= tmp & strBit
            num=num \ x
        Loop While Not num <= x
        
        
        If num = 36 Then
            num = "+"
        ElseIf num = 37 Then
            num = "="
        ElseIf num > 37 Then
            num = Chr(97+num-37-1)
        ElseIf num > 9 Then 
            num = Chr(65+num-9-1)
        End If
            
        Dec2Other=num&strBit
        Do While Instr(Dec2Other,"0") = 1
            Dec2Other = Mid(Dec2Other,2,Len(Dec2Other))
        Loop
    End Function

    ' x(x=2,8,16,32,64)进制转十进制
    ' 结果为整型数字
    Function Other2Dec(num,x)
        Dim tmp
        Dim cDecNumber : cDecNumber=0
        For inum=0 to len(num)-1
            tmp = mid(num,len(num)-inum,1)
            If Not isNumeric(tmp) Then
                If tmp = "+" Then
                    tmp = 36
                ElseIF tmp = "=" Then
                    tmp = 37
                ElseIf asc(tmp) > 97 Then
                    tmp = (asc(tmp)-97+37+1)
                ElseIf asc(tmp) > 65 Then
                    tmp = (asc(tmp)-65+9+1)
                End If
            Else
                tmp = Int(tmp)
            End If
            cDecNumber=cDecNumber + x ^ inum * tmp
        Next
        Other2Dec=cDecNumber
    End Function
End Class

' 附加类,用于防止与同名内置函数的冲突导致的堆栈溢出
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Class HDOBTools_Hack
    Function Dec2Oct(num)
        Dec2Oct = Oct(num)
    End Function
    Function Dec2Hex(num)
        Dec2Hex = Hex(num)
    End Function
End Class
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%>

 

posted on 2012-09-28 10:32  Dream Young  阅读(739)  评论(0编辑  收藏  举报