Search code examples
ibm-midrangecontrol-language

Is it possible to convert hex format character back to decimal in a CL program?


I have a RPG program A and a CL program B. A calls B with a data structure as entry parameter. This data structure contains characters as well as packed decimals. The entry parameter in B is declared as a character variable, so when the entry parameter is passed in from A, the decimal values end up in their hex-decimal format. I need their original decimal values in B. Is there any way to convert these hex character strings back to decimal in B?


Solution

  • You don't need to convert. Define the decimal field over the appropriate location in your data structure.

    PGM (&STRUCT)
       DCL &STRUCT      *CHAR 12
       DCL &NAME        *CHAR 10   STG(*DEFINED) DEFVAR(&STRUCT)
       DCL &NBR         *DEC (3 0) STG(*DEFINED) DEFVAR(&STRUCT 11)
    

    The *DEFINED storage method works well when the position is fixed. In a situation where the location may vary, then use *BASED storage.

    In this example there is a numeric value in a position determined by a format parameter. The value will be incremented by 1 or two.

    PGM (&STRUCT)
       DCL &STRUCT     *CHAR 80
       DCL &FMT        *CHAR 4
       DCL &P1         *PTR
       DCL &NUM        *DEC (3 0) STG(*BASED)
       ...
       CHGVAR &FMT %SUBSTR(&STRUCT 1 4)
       CHGVAR  &P1  %ADDRESS(&STRUCT)
       SELECT
          WHEN (&FMT = 'ONE ')  THEN(DO)
             CHGVAR %OFFSET(&P1) 20       /* POS 21 IN STRUCT */
             CHGVAR &NUM (&NUM + 1)       /* INCREMENT NUMBER BY 1 */
             ENDDO
          WHEN  (&FMT = 'TWO ')  THEN(DO)
             CHGVAR %OFFSET(&P1) 40       /* POS 41 IN STRUCT */
             CHGVAR &NUM (&NUM + 2)       /* INCREMENT NUMBER BY 2 */
             ENDDO
          OTHERWISE DO
             /* EXIT WITH ERROR */
             ...
             ENDDO
          ENDSELECT
    

    See Variables in CL commands