Converting comp-3 back to a human readable format

11.4k Views Asked by At

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)?

4

There are 4 best solutions below

0
On BEST ANSWER

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

1
On

When you store a number in a normal PIC 9(4) field, the values stored are using the hex codes for those characters.

example:

1234 would be stored as F1F2F3F4 If you were to put that into a COMP field it would be stored as 00001234 so that means you would have the following hex bytes 00 00 12 34. If you wanted to convert it back, you will probably need to convert that field back to EBCDIC (or what ever encoding you were using on the COBOL side), and look at the actually hex values for that field.

0
On

Comp-3 is a packed format. S9(4) comp-3 occupies 3 bytes. +1234 will be stored as x'01234C' in hex format. While -1234 will be stored as x'01234D'. As you can see, the first 4 bits of the first byte are always 0 in this case, so basically you wasted 1 digit. S9(5) and S9(4) occupy the same bytes. Apparently you don't really use 3270 simulator, and I don't know about notepad++, I think ultraedit can show you the hex format of a file. PS: I would suggest you to use COMP instead of COMP-3. COMP is binary mode, its calculation efficiency is 3 times of COMP-3.

As to your second question. Break the string into bits. Compensate 0000 to each 4 bits except the last 4 bits will give you the human-readable result. The last 4 bits, depends on it's C or D, is the + or - sign.

0
On

If you know that the value is not decimal, try to move it to a PIC 9(9).