Is that possible to generate a COMP-3 Field in Java?
I'm doing a process that a I need to send a txt file to Mainframe and for bether processing it used a COMP-3 field, to avoid a conversion process in COBOL, I'm trying to generate this format in JAVA, is that possible?
Its for a batch process, that a I need to sendo to Mainframe many registries with value, and the value I want to generate in COMP-3 in JAVA.
Is that possible to do the reverse to? Unpack a COMP-3 field in java, to BigD or String.
How can I do this?
Thanks!
A COBOL COMP-3 is a packed decimal field. The last byte is the sign field. The value x'C' represents a positive value, the value x'D' represents a negative value, and the value x'F' represents an unsigned value.
A COMP-3 field has a specific number of digits and an implied decimal point. You need to know the number of digits and where the implied decimal point is, so your numbers will be interpreted correctly in the COBOL program.
A COMP-3 field always has an even number of bytes. Most COBOL developers recognize this and create COMP-3 PICTURE clauses with an odd number of digits.
Let's take an example of 200 and a COBOL PICTURE of +999 COMP-3 and convert it to a packed decimal.
Let's take an example of -125.125 and a COBOL PICTURE of -999V999 COMP-3 and convert it to a packed decimal.
Basically, you take each digit of the
int
ordouble
, move the digit into a byte, and shift the byte into the proper position.I went ahead and wrote two conversion methods since it's obvious that these conversions are so complex that only an experienced COBOL/Java developer can possibly code them.
Here is one of my test results.
The first value is the input
double
.The second value is the packed decimal String, displayed as a series of hexadecimal values.
The third value is the result of converting the packed decimal String back to a
double
.Here's the complete runnable code.