Converting comp-3 back to a human readable format

jdstaerk picture jdstaerk · Jan 23, 2017 · Viewed 8.8k times · Source

I have the information, that a PIC S9(4) field is encoded with comp-3. I also have a data file which contains data (in my case about a customer). When I open the data file using notepad++, it show's me chars like "DLE", "BEL" or "NUL", which leads me to this table. First off, can you explay to me, what "comp-3" does? And how do I convert it back into a human readable format / data using c# for example (having both files as an input)?

Answer

Bruce Martin picture Bruce Martin · Jan 23, 2017

Comp-3 format

in Comp-3 format

value     comp-3 (hex)
 123        x'123c'
-123        x'123d'

There are a lot existing questions https://stackoverflow.com/search?q=%22comp-3%22+cobol

If this is a mainframe file and has been converted to ASCII; it will not be use-able !!!. The 2nd answer in COBOL COMP-3 number format issue gives a good example of corruption of a Comp-3 when doing a ascii conversion.

Mainframe issues

If it is from the mainframe, The problems you face:

  1. You must do a binary Transfer
  2. You have to read Mainframe file structures (e.g. FB records will be fixed length records; no cr/lf).
  3. you need to convert the packed decimal.

Mainframe Options

The best option is to do the conversion on the mainframe / Cobol and transfer a Text file.

Alternatively There are projects like coboltocsv which will convert a Cobol file to Csv using a Cobol Copybook.


Converting comp-3

As far as converting comp-3 that is easy enough. There are 2 approaches

  1. Convert the Hex to a Hex-String (i.e. convert x'123c' --> "123C) and check the last sign character. See COMP-3 data unpacking in Java (Embedded in Pentaho)

  2. Break the field into a series of nybles