Interpreting COMP-3 Packed Decimal Fields into numeric values

Ham picture Ham · Apr 28, 2009 · Viewed 32.6k times · Source

I am creating an SSIS package to read in unpacked data from a series of copybook files. I am unsure of the correct interpretation of the following field definitions and was hoping someone would know:

FIELD-NAME-1 PIC S9(15)V9(3) COMP-3.
FIELD-NAME-2 PIC S9(3)V9(8) COMP-3.
FIELD-NAME-3 PIC S9(3)V9(6) COMP-3.

The data is stored in fixed width text. The data for the above fields has the following lengths:

FIELD-NAME-1: 19 FIELD-NAME-2: 11 FIELD-NAME-3: 9

I am unsure how to interpret the decimal place and sign.

Any help would be greatly appreciated.

Kind Regards, Ham

Answer

dna123 picture dna123 · Apr 28, 2009

Here is a little different attempt at answering your questions.

PIC S9(15)V9(3) COMP-3 looks like this in the file:

    00 00 00 00 00 00 00 00 00 0F

If the value was -4568248.323, it would be:

    00 00 00 00 04 56 82 48 32 3D

This doesn't help you, but may help others. Unpacked the previous value would look like:

F0 F0 F0 F0 F0 F0 F0 F0 F0 F4 F5 F6 F8 F2 F4 F8 F3 F2 D3 (or F3 as the last byte, therefore losing the sign)

This field has 15 (actually 16) digits before the decimal point and 3 after.

Although it only requests 18 digits (15+3), it gets 19 to make it an even length field with the sign (one digit added to the front to make it 10 bytes long on the file). Best practice is to always make packed fields an odd length to avoid this confusion.

** The last letter denotes the sign, C & F are positive, D is negative. For your program, check for negative (D) and if not, treat as positive.

** The 'V' is an implied decimal point. it doesn't exist on the file, but COBOL knows that it's there for rounding and such. You need to programmatically account for it. There is nothing in the file to help you identify where it is or if it even exists.

The other two fields are already odd lengths, so when packed, with the sign, they can be stored in an even length amount of space.

Any other questions, edit your question or ask in the comments and someone will try to answer them for you.