Search code examples
cobol

How to move COMP-5 values to a Numeric field?


I am trying to move a COMP-5 variable(which I am receiving from some other system) to a numeric field. I have noticed when I display the COMP-5 variable I can see the value but when I try to move it the value in COMP-5 variable becomes zeros.I don't have any experience working with COMP-5. Can someone help me with this?

Code:

   09 O-Xid.                                                
     12 O-Xid2-length                   PIC S9999 COMP-5 SYNC.                                                       
     12 O-Xid2                          PIC X(255).   
   09 WS-O-Xid.                                             
     12 WS-O-Xid2-length                PIC 9999.        
     12 WS-O-Xid2                       PIC X(255).      
      
                                                              
MOVE O-Xid2-length         TO WS-O-Xid2-length       
MOVE O-Xid2                TO WS-O-Xid2              

Solution

  • MOVE as you've used does any necessary conversions between any numeric USAGE, as long as the data is valid.

    The code misses the actual DISPLAY statement, I assume you've tested for valid data with DISPLAY O-Xid2-length (please specify the output).

    The most likely reason that the target does not contain the source value would be:
    the COBOL environment you use (you've neither specified the compiler not the options you used) doesn't truncate COMP-5 values according to ANSI/ISO - so it may contain "10000" and is then truncated on the MOVE because the target can't hold that value (standard truncation happening here keeps only the last 4 digits).
    All other cases get to "there is not the data in the field that you think" - again: please specify both the ´DISPLAY` statement and the result.

    Additional info to TRUNC(BIN): according to the docs:

    • BINARY sending fields are handled as halfwords, fullwords, or doublewords when the receiver is numeric
    • DISPLAY will convert the entire content of binary fields with no truncation.

    DISPLAY would also show a value like 30000, I think the MOVE would in this case result to a zero value.


    For other usages, it would be possible that the value stored in the variable is actually not valid data, but this does not apply to BINARY (or COMP-5 items). In this case the COBOL environment used could do some auto-correction on DISPLAY, but on MOVE just change the invalid value to ZERO; to check that you'd need to use either a debugger or otherwise hex-dump the value received.