midrange.com code scratchpad |
Name:
Calculate UPC Check Digit
|
Scriptlanguage:
Plain Text
|
Tabwidth:
4
|
Date:
08/16/2010 07:33:10 pm
|
IP:
Logged
|
|
Description:
The user was to lazy to give a description
|
Code:
- <prototype>
- *--------------------------------------------------
- * Procedure name: Utl_CalcUPCChkD
- * Purpose: Calculate the UPC check digit
- * Returns: Check digit
- * Parameter: UPC
- *--------------------------------------------------
- D UTL_CalcUPCChkD...
- D PR 1P 0 EXTPROC('CALCUPCCHKD')
- D UPC 15P 0 Value
- </prototype>
- <code>
- P*--------------------------------------------------
- P* Procedure name: CalcUPCChkD
- P* Purpose: Calculate the UPC check digit
- P* Returns: Check digit
- P* Parameter: UPC
- P* Description: This calculation was pulled from
- P* from a document labeled SIL2000.PDF. Its the
- P* UCS Standard Interchange Language reference.
- P* The algorithm is located in Appendix B.
- P*--------------------------------------------------
- P CalcUPCChkD B EXPORT
- D CalcUPCChkD PI 1P 0
- D UPC 15P 0
-
-
- D* Local fields
- D CheckDigit S 1P 0
- D Step1Val S 5P 0
- D Step2Val S 5P 0
- D Step3Val S 5P 0
- D Step4Val S 5P 0
-
- D DS
- D UPCField 15S 0
- D UPCAr 1S 0 DIM(15) Overlay(UPCField)
-
- /FREE
-
- // Array Elm: 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15
- // Positions: 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01
- // 0 0 0 0 5 1 0 0 0 0 0 0 1 1 ? <- Check Digit
- UPCField = UPC*10; // move to left by one
- // Starting from position 2 of the number, add up the
- // values in the even numbered positions.
- Step1Val = UPCar(14) +UPCar(12) +UPCar(10) +UPCar(08)
- +UPCar(06) +UPCar(04) +UPCAr(02);
- Step2Val = Step1Val * 3;
- // Starting from position 3 of the number, add up the values
- // in the odd numbered positions. That is, add up all the
- // numbers left over from step 1. You're skipping position 1
- // because position 1 is the check digit.
- Step3Val = UPCar(13) +UPCar(11) +UPCar(09) +UPCar(07)
- +UPCar(05) +UPCar(03) +UPCar(01);
- Step4Val = Step2Val + Step3Val;
- Monitor;
- CheckDigit = 10 - %Rem(Step4Val:10);
- On-error *All;
- CheckDigit = 0;
- EndMon;
-
- RETURN CheckDigit;
-
- /END-FREE
- P CalcUPCChkD E
- </code>
|
|
|