      ******************************************************************
      * Author: Jay Moseley
      * Date: May 19, 2016
      * Purpose: Encode a numeric field into base 36 from base 10. The 
      *          maximum base 10 value that may be encoded into a 6 
      *          character field is: 2176782335 (output=ZZZZZZ); if the
      *          base 10 value passed is larger than 2176782335, the
      *          return field will be '000000' and RETURN-CODE is +8.
      * Tectonics: cobc -m base36.cbl
      ******************************************************************
       IDENTIFICATION DIVISION.

       PROGRAM-ID. toBase36.
       ENVIRONMENT DIVISION.

       CONFIGURATION SECTION.

       INPUT-OUTPUT SECTION.

       DATA DIVISION.

       FILE SECTION.

       WORKING-STORAGE SECTION.

       01  WORK-FIELDS.
           02  WF-DIVIDEND             PIC 9(10).
           02  WF-QUOTIENT             PIC 9(10).
           02  WF-REMAINDER            PIC S9(4) COMP.
           02  WF-DIG36                PIC X(36) VALUE
               '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'.
           02  WF-X                    PIC S9(4) COMP.

       LINKAGE SECTION.

       77  PF-INPUT                    PIC 9(10).
       77  PF-OUTPUT                   PIC X(6).

       PROCEDURE DIVISION USING PF-INPUT, PF-OUTPUT.

       MAIN-PROCEDURE.

           MOVE PF-INPUT TO WF-DIVIDEND.
           MOVE ZEROS TO PF-OUTPUT.
           MOVE +6 TO WF-X.

           IF PF-INPUT > 2176782335
             MOVE +8 TO RETURN-CODE
           ELSE
             MOVE +0 TO RETURN-CODE

             PERFORM UNTIL WF-DIVIDEND = 0
               DIVIDE 36 INTO WF-DIVIDEND
                 GIVING WF-QUOTIENT
                 REMAINDER WF-REMAINDER
               ADD +1 TO WF-REMAINDER
               MOVE WF-DIG36(WF-REMAINDER:1) 
                 TO PF-OUTPUT(WF-X:1)
               SUBTRACT +1 FROM WF-X
               MOVE WF-QUOTIENT TO WF-DIVIDEND
             END-PERFORM
           END-IF.

           GOBACK.
      * - - - - - - - - - - - - - - - - - - - - - - - PROGRAM EXIT POINT

       END PROGRAM toBase36.

      ******************************************************************
      * Author: Jay Moseley
      * Date: May 19, 2016
      * Purpose: Decode a numeric field from base 36. The maximum base
      *          10 value that may be encoded into a 6 character field
      *          is 2176782335 (output=ZZZZZZ).
      * Tectonics: cobc -m base36.cbl
      ******************************************************************
       IDENTIFICATION DIVISION.

       PROGRAM-ID. fromBase36.
       ENVIRONMENT DIVISION.

       CONFIGURATION SECTION.

       INPUT-OUTPUT SECTION.

       DATA DIVISION.

       FILE SECTION.

       WORKING-STORAGE SECTION.

       01  WORK-FIELDS.
           02  WF-PLACE                PIC S9(4) COMP.
           02  WF-VALUE                PIC S9(4) COMP.
           02  WF-DIG36                PIC X(36) VALUE
               '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'.
           02  WF-X                    PIC S9(4) COMP.

       LINKAGE SECTION.

       77  PF-INPUT                    PIC X(6).
       77  PF-OUTPUT                   PIC 9(10).

       PROCEDURE DIVISION USING PF-INPUT, PF-OUTPUT.

       MAIN-PROCEDURE.

           MOVE 0 TO PF-OUTPUT.
           MOVE +6 TO WF-X.
           MOVE +0 TO WF-PLACE.

           PERFORM VARYING WF-X FROM 6 BY -1
                     UNTIL WF-X = 0
             MOVE +0 TO WF-VALUE
             INSPECT WF-DIG36
               TALLYING WF-VALUE
               FOR LEADING CHARACTERS
               BEFORE INITIAL PF-INPUT(WF-X:1)
             COMPUTE PF-OUTPUT = PF-OUTPUT +
                     (WF-VALUE * 
                     (36 ** WF-PLACE))
             ADD +1 TO WF-PLACE
           END-PERFORM.

           MOVE +0 TO RETURN-CODE.
           GOBACK.
      * - - - - - - - - - - - - - - - - - - - - - - - PROGRAM EXIT POINT

       END PROGRAM fromBase36.

