Packed Decimal (Comp3 in Cobol)
- +123 is stored as x'123C'
- -123 is stored as x'123D'
- unsigned 123 is x'123F'
Do you have a Cobol Copybook ???, if you do, see JRecord Question.
JRecord also has
- Csv to Cobol program (Convert a Csv file to Cobol using a Cobol Copybook)
- Xml-to-Cobol program (Convert a Xml file to Cobol using a Cobol Copybook)
Alternatives to JRecord are
- Cobol2J
- legstar
- various commercial products
Note: I am the author of JRecord
Converting a number to Packed Decimal is pretty easy and there are a number of ways to do it
Convert to String
one option is
- Convert the number to a String
- Add the sign Character to the end of the String
- Convert the String to Bytes (Hex-String)
Java Code to convert an integer to Packed-Decimal
String val = Integer.toString(Math.abs(value));
if (value < 0) {
val = val.substring(1) + "D"
} else {
val += "C";
}
byte[] bytes = (new BigInteger(val, BASE_16)).toByteArray();
Similar Question
How to convert unpacked decimal back to COMP-3?