Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Translate formula into VFP

Status
Not open for further replies.

mjcmkrsr

Technical User
Nov 30, 2010
840
Hi,

Would anybody be as kind as to translate this code into VFP. I'm absolutely clueless and would appreciate a lot.

Many thanks.

MarK



Code:
/**
 * Apply Luhn algorithm to compute check digit
 * This algorithm is used to compute the first check digit
 * during extended unique id generation
 *
 * @author Ciedmdr
 */
public class CheckDigitLuhn {
    /**
     * Computes the checksum C according Luhn algorithm
     * @param String charset to compute Luhn check digit
     * @return the check digit
     */
    public static int computeCheckDigit(String iNumber ) {
        int checkSum = 0;
        int weight = 0;
        int weightedDigit = 0;
        for(int pos=0;pos<iNumber.length();pos++) {
            weight    = (pos%2==0)?2:1;
            weightedDigit = Character.digit(iNumber.charAt(iNumber.length()-pos-1),10) * weight;
            checkSum += (weightedDigit>9?weightedDigit-9:weightedDigit); 
        }
        return (10 - checkSum%10) % 10;
    }
    /**
     * Verify the number in parameter (11 DIGITS + Luhn check digit = 12 DIGITS)
     * @param iNumber
     * @return true if checked
     */
    public static boolean checkDigit( String iNumber ) {
        int checkSum         = 0;
        int weight             = 0;
        int weightedDigit     = 0;
        for(int pos=0;pos<iNumber.length();pos++) {
            weight             =     (pos%2==0)?1:2;
            weightedDigit    =     Character.digit(iNumber.charAt(iNumber.length()-pos-1),10) * weight;
            checkSum         +=     (weightedDigit>9?weightedDigit-9:weightedDigit); 
        }
        if (checkSum % 10 == 0)
            return true;
        else
            return false;
    }
}

/**
 * Apply Verhoeff algorithm to compute check digit
 * This algorithm is used to compute the second check digit during extended unique id generation
 *
 * @author Ciedmdr
 */
public class CheckDigitVerhoeff {
    private static int inv( int iPos ) {
        int invTable[] = {0,4,3,2,1,5,6,7,8,9};
        return invTable[iPos];
    }
    private static int d(int j, int k) {
        int dTable[][] =  {    {0,1,2,3,4,5,6,7,8,9},
                    {1,2,3,4,0,6,7,8,9,5},
                    {2,3,4,0,1,7,8,9,5,6},
                    {3,4,0,1,2,8,9,5,6,7},
                    {4,0,1,2,3,9,5,6,7,8},
                    {5,9,8,7,6,0,4,3,2,1},
                    {6,5,9,8,7,1,0,4,3,2},
                    {7,6,5,9,8,2,1,0,4,3},
                    {8,7,6,5,9,3,2,1,0,4},
                    {9,8,7,6,5,4,3,2,1,0}};
        return dTable[j][k];
    }
    private static int p(int i, int Ni) {
        int pTable[][] = {    {0,1,2,3,4,5,6,7,8,9},
                    {1,5,7,6,2,8,3,0,9,4},
                    {5,8,0,3,7,9,6,1,4,2},
                    {8,9,1,6,0,4,3,5,2,7},
                    {9,4,5,3,1,2,6,8,7,0},
                    {4,2,8,6,5,7,3,9,0,1},
                    {2,7,9,3,8,0,6,4,1,5},
                    {7,0,4,6,9,1,3,2,5,8}};
        return pTable[i % 8][Ni];
    }
    /**
     * Computes the checksum C as
     * C = inv(F_n (a_n)×F_(n-1) (a_(n-1) )×… ×F_1 (a_1 ) )
     * (with × being the multiplication in D_5)
     * @param String charset to compute Verhoeff check digit
     * @return the check digit
     */
    public static int computeCheckDigit(String iNumber ) {
        int checkSum = 0;
        for(int pos = 0; pos < iNumber.length(); pos++) {
            checkSum = d(checkSum,p(pos+1, Character.digit(iNumber.charAt(iNumber.length()-pos-1),10)));
        }
        return inv(checkSum);
    }
    /**
     * Verify the number in parameter (11 DIGITS + Verhoeff check digit = 12 DIGITS)
     * The verification computes and verified the following equation
     *    (F_n (a_n )×F_(n-1) (a_(n-1) )×…×F_1 (a_1 )×C) = 0
     * @param iNumber
     * @return true if checked
     */
    public static boolean checkDigit(String iNumber) {
        int checkSum = 0;
        for(int pos = 0; pos < iNumber.length(); pos++) {
            checkSum = d(checkSum,p(pos, Character.digit(iNumber.charAt(iNumber.length()-pos-1),10)));
        }
        if( checkSum == 0) {
            return true;
        }
        return false;
    }
}
 
Thanks for the link, Mark. I see now where you got your original code from. I suppose it would have been more useful if the CCSS had published the algorithm as pseudo-code rather than in a particular language (especially as they don't even tell you which language it is), but at least you had something to work on.

Also, the changeover doesn't look quite as rushed as I thought. Existing employees will simply append the two check digits to their existing identifiers, and there will have been about a year to plan for the change.

Still, I hope you manage to finish it in time, and you won't let it spoil your new year.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Hi Mike,

ad 1) You're right
ad 2) The changeover for administrations is as of January 1st, 2014. Private employers will have time till June 30st, 2014 to adapt. As from July 2014 the new SSN will be compulsory.
ad 3) I finished and will have my champagne tonight

HNY

Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top