0

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)

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Abhijit Marne
  • 65
  • 2
  • 3
  • If it is not directly sported... start be reading format documentation... Not really sure what you are looking for from SO. Maybe just use search - since you probably can't use search engine like https://www.bing.com/search?q=Java+String+to+EBCDIC here is possible result http://stackoverflow.com/questions/368603/convert-string-from-ascii-to-ebcdic-in-java – Alexei Levenkov Dec 17 '15 at 07:07
  • Do you have a format description for your file? Do you know *exactly* where the COMP-3 fields are? It would be great if you'd provide an example line and description – Jan Dec 17 '15 at 08:16
  • Your example is NOT packed decimal; it is ALMOST *zoned* decimal, where the last digit (usually) or first digit (sometimes) -- but not one in the middle -- is Cx or Dx. http://stackoverflow.com/a/10545291/2868801 – dave_thompson_085 Dec 17 '15 at 12:35

1 Answers1

0

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?

Community
  • 1
  • 1
Bruce Martin
  • 10,358
  • 1
  • 27
  • 38
  • thank you for the answer but my file contain characters also along with the integers something like "Hi N456 The End" and this string i want to convert in format like "C1 60 C1 4B F2 F5 F9 4B F5 F0 F0". – Abhijit Marne Dec 17 '15 at 11:43
  • What has packed decimal got to do with anything ???; Have a look at the OutputStreamWriter class, in particular characterset parameter – Bruce Martin Dec 17 '15 at 21:13