ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Character to Zoned decimal conversion

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Character to Zoned decimal conversion

    I have a CL program that calls RPGLE procedure.

    RPGLE procedure has field defined as 5S 0 (zoned decimal). In CL, i have a file variable which is of 10 characters (though value cannot exceed 5 characters). I need a way to pass this to the RPGLE procedure. I tried %BIN, %DEC, etc but then end with error. When i tried to copy 10 digit character field to 5 digit character field, the field "100" from file becomes "100__" (2 spaces at end). This causes decimal data crash at RPGLE procedure.

    Any efficient way to handle this?

  • #2
    Hi, Sri.

    If the number won't be negative, you can pass a 5-byte character value to it. Zoned decimal looks just like character unless the number is negative.

    To keep it clean with leading zeros, first copy the 10-byte variable to a packed-decimal variable, then the packed decimal value into a 5-byte character variable.

    Code:
    dcl  &CharValue     *char   10   
    dcl  &DecValue      *dec     5   
    dcl  &ZonedValue    *char    5   
    
    chgvar  &CharValue    '  123  '  
    
    chgvar  &DecValue     &CharValue
    chgvar  &ZonedValue   &DecValue 
    
    call whatever   parm( . . . &ZonedValue . . . )
    If the number can be negative, we'll have to think of something else.

    Ted

    Comment


    • #3
      Hi TedHolt, Cant thank you enough. Was scratching my head for quite some time about this conversion. With Packed decimal, logic was straight forward. Never had idea about Zoned decimal. Your solution works perfectly. Thanks again

      Comment

      Working...
      X