0

Suppose I have a PS input file that contains 9-byte long data. The input data would be numbers that are necessarily left aligned. Hence, in case the number in the input file is smaller than 9 bytes, the number would be trailed by spaces.
When I read such a number into a 9(9) variable and DISPLAY the value the value displayed would be the number followed by the spaces (checked with SET HEX ON in the spool).
But instead when I MOVE the value from the 9(9) variable into an S9(9) COMP variable and then DISPLAY its value, the value displayed would be some random numeric value.
My question here is, how does COBOL interpret/convert the value for an S9(9) COMP variable in the above scenario?

ikartik90
  • 2,695
  • 6
  • 26
  • 38

1 Answers1

2

It's a computer, the results you get are far from random.

What did you think would happen?

You are MOVEing something which is not numeric, but which you have defined as numeric, to a binary field. You will have a value which relates to your data, just not what you expect.

You have compiler option NUMPROC(NOPFD). If you had NUMPROC(PFD) you would have got an S0C7 abend,

You should find that all your trailing spaces get treated as zero, with NUMPROC(NOPFD).

A zoned-decimal, prior to a calculation, or, as in your case, a conversion to binary, is "packed". Which means all the zones are tossed away, the sign and final digit are reversed.

So, in the packed number you only get the numerics and the sign.

As long as all the numerics are 0-9 and the sign is A-F, no S0C7. Garbled results, but no S0C7.

If we consider your final two trailing spaces, X'4040'. You have NUMPROC(NOPFD) so the compiler will "fix" the sign for you (to an F in this case). Toss the zones (so the first 4 goes) swap the last byte (become X'04'), fix the sign (becomes X'0F') and converts that value to binary (which is successful). You have turned spaces into zeros.

If you used NUMPROC(PFD) the sign fixing would not occur, and the convert to binary (CVB) would give you a S0C7 abend.

Bill Woodger
  • 12,968
  • 4
  • 38
  • 47