lucamauri
12/22/2013 - 9:56 PM

This module compute the bank account checksum known as CIN

This module compute the bank account checksum known as CIN

Module modCIN
    Friend Const CINLetters As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ-. "
    Friend Const CINDigits As String = "0123456789"

    Friend Const Divisor As Integer = 26

    Friend CINEvenList As Integer() = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28}
    Friend CINOddList As Integer() = {1, 0, 5, 7, 9, 13, 15, 17, 19, 21, 2, 4, 18, 20, 11, 3, 6, 8, 12, 14, 16, 10, 22, 25, 24, 23, 27, 28, 26}

    Function CalcCIN(ByVal ABI As String, ByVal CAB As String, ByVal Account As String) As String
        Dim ProperString As String
        Dim GrandTotal As Integer
        Dim Position As Integer
        Dim CurrChar As Char

        If ABI.Length <> 5 Or CAB.Length <> 5 Or Account.Length > 12 Or Account.Length < 1 Then
            Return "Lenght error in input data"
        End If

        If Account.Length = 12 Then
            ProperString = ABI & CAB & Account.ToUpper
        Else
            ProperString = ABI & CAB & Account.PadRight(12, " ").ToUpper
        End If

        GrandTotal = 0

        For i = 0 To 21
            CurrChar = ProperString(i)

            Position = CINDigits.IndexOf(CurrChar)
            If Position < 0 Then
                Position = CINLetters.IndexOf(CurrChar)
                If Position < 0 Then
                    Return "Invalid character found in position " & i
                End If
            End If

            If i Mod 2 = 0 Then
                GrandTotal += CINOddList(Position)
            Else
                GrandTotal += CINEvenList(Position)
            End If
        Next
        Return CINLetters.Substring(GrandTotal Mod Divisor, 1)
    End Function
End Module