Interpreting COMP-3 Packed Decimal Fields into numeric values

33,457

Solution 1

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.

Solution 2

Usually COMP-3 fields consist of BCD digits packed into bytes two at a time, each digit using a nibble (4 bits). The last digit goes in the upper nibble of the last byte. The lower nibble of the last byte has 13 if the number is negative, and something else, (usually 12) if positive. The decimal point is implied.

For example, -1.2 looks like this in hex, the final D is the negative sign.

   01 2D

12.345 is:

   12 34 5C

Solution 3

Here we go:

PIC is "picture"
S9(15) means a 15 digit numeric signed field: S for sign, 9 is numeric, (15) is length. V is the decimal position 9(3) is a three digit numeric

and COMP-3 is BCD encoded decimal. Each nybble (half-byte) of the field is a decimal value in binary, so

0b01110110 (duh)

is "76".

18 digits requires 9 bytes, the sign is the low nybble of the low order byte.

Which worries me, those should require 10 bytes.

Here's a nice article on it.

Share:
33,457
Admin
Author by

Admin

Updated on July 09, 2022

Comments

  • Admin
    Admin almost 2 years

    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