博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

有趣的算法

Posted on 2011-01-31 00:26  ☆Keep★Moving☆  阅读(157)  评论(0编辑  收藏  举报

當序號為99後,會變成0A.0B.......0Z...1A..1B......1Z....2A

Public Function ID2INT(ByVal strData As String) As Integer
   
    Dim strX As String
    Dim strY As String
   
    If Len(Trim(strData)) <> 2 Then
        ID2INT = -1
    Else
        If IsNumeric(strData) Then
            ID2INT = CInt(strData)
        Else
            strX = Left(Trim(strData), 1)
            strY = Right(Trim(strData), 1)
           
            If IsNumeric(strX) Then
                ID2INT = CInt(strX) * 26 + (Asc(strY) - Asc("A")) + 100
            Else
                ID2INT = (10 + Asc(strX) - Asc("A")) * 26 + (Asc(strY) - Asc("A")) + 100
            End If
        End If
    End If
Exit Function
ErrHandle:
    If Len(g_objSystem.strErrorFunction) = 0 Then
        g_objSystem.strErrorModule = MC_THISMODULE
        g_objSystem.strErrorFunction = "ID2INT"
    End If
    Call Err.Raise(Err.Number)
End Function

Public Function INT2ID(ByVal intData As Integer) As String
    Dim intX As Integer
    Dim intY As Integer
    If intData >= 0 Then
        If intData < 100 Then
            INT2ID = format(intData, "00")
        Else
            intX = (intData - 100) \ 26
            intY = (intData - 100) Mod 26
            If intX < 10 Then
                INT2ID = intX & Chr(intY + Asc("A"))
            ElseIf intX > (Asc("Z") - Asc("A") + 10) Then
                INT2ID = vbNullString
            Else
                INT2ID = Chr(Asc("A") + (intX - 10)) & Chr(intY + Asc("A"))
            End If
        End If
    Else
        INT2ID = vbNullString
    End If
Exit Function
ErrHandle:
    If Len(g_objSystem.strErrorFunction) = 0 Then
        g_objSystem.strErrorModule = MC_THISMODULE
        g_objSystem.strErrorFunction = "INT2ID"
    End If
    Call Err.Raise(Err.Number)
End Function