I am confused about 9
(display format) and S9
(unpacked numeric) type. I was reading copybook output from DB2 stored procedure using Java
* copy book
05 ABC PIC X(01).
05 XYZ PIC +9(09).
Using the below Java code I was able to read the values properly. The sign of the number also very clear to handle as it was either -
or +
// java code
cobolResults.subString(1,1);
cobolResults.subString(2,9);
Now the copybook is changed to give unpacked numeric like below:
05 ABC PIC X(01).
05 XYZ PIC S9(09).
I am not sure what would be the correct logic in Java to read the number with proper sign?
To start with there are packages from IBM & Legstar to generate Java classes from Cobol Copybooks. My own package JRecord is could also be used, but it is orientated to files rather than on-line processing.
Basically the last character of the field holds the sign + number. I am guessing the data is coming from the mainframe; So for US - Ebcdic (CP037 / IBM237) the last digit will be
So for +123 it will be 00000012C (C = +3) and -123 will be 00000012L.
To make matters worse, +0 and -0 are different in different EBCIDIC dialects and ASCII as well. So you either need to know exactly which version of Ebcidic is being used or you need to do the conversion at byte level.
The fromZoned method in JRecord Conversion does the conversion:
But it is easier to do at byte level it should be like the following (Code is not tested though):