How to convert a Simple Java String to EBCDIC with packed decimal format

3.6k Views Asked by At

i have to convert all the data in file to EBCDIC with packed decimal format.

all the data in the file is in simple text format.

As per my knowledge we will need to convert the ASCII to EBCDIC Cp1047 or some other format first and then apply “packed decimal” logic/code.(may be i am wrong)

the converted format should be like "C3 C5 40 F0 C9 F8"(i.e. EBCDIC packed decimal format)

1

There are 1 best solutions below

2
On

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

  1. Convert the number to a String
  2. Add the sign Character to the end of the String
  3. 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?