You are on page 1of 3

# Comp-3 To Comp - Decimal Conversions

Introduction: A COBOL program involves the interaction of items with different data formats and the compiler converts these items either temporarily, for comparisons and arithmetic operations or permanently, for assignment to the receiver in a MOVE or COMPUTE statements. A conversion is simply a move of a value from one data item to another data item. Compiler option TRUNC(STD), TRUNC(OPT) or TRUNC(BIN) can be used to indicate how binary data (BINARY, COMP, or COMP-4) gets truncated. Different examples given this document holds true ONLY for the compiler option TRUNC(BIN). Aim of the document: a) How the data is stored in the binary/COMP/COMP-3 packed fields? b) Behaviour of the COMP/COMP-3 packed fields when data is transferred across COMP/COMP-3 fields and other non-binary fields. To elaborate more on the above mentioned points we will go through few of the examples given below: Example 1: Input Variable has data OUTPUT Value passed PIC Claus e 9(10) 9(10) Comp3 X(10) S9(10) S9(10) Comp3 S9(09) Value Received (Display) 1234567891 1234567891 234567891 123456789 A 1234567891 23456789A OBSERVATION

stored with the usage as COMP. INPUT PIC Claus e

ü ü ? ? ü ?

S9(09) COMP

123456789 1

Points to be noted: Case 3: In this case first integer gets truncated while moving from S9 (09) COMP to X (10). Binary format (COMP) numbers occupy 2, 4 or 8 bytes of storage and the leftmost bit is used as the operational sign. In this case ‘Sign’ is associated with the first integer value i.e. ‘1’. There are 2 ways to get the correct value in the target field: • Use one intermediate variable of 9(10). S9 (09) COMP à 9(10) and then 9(10) à X (10) • Change the Picture clause of the source variable to S9 (10) COMP. In this way ‘Sign’ gets freed up with the first integer value i.e.

S9 (10) COMP à X (10) (=1234567891) (=1234567891) Case 4 & 6: In case 4, it is the display which is not showing the correct value but actually the correct value is stored internally. In the decimal numeric type ‘Sign’ is always associated with the last byte. In case 6, first integer gets truncated because the target picture clause is not big enough to accommodate the incoming data. Note: Binary items with nine or more digits require more handling by the compiler. Example 2: Input Variable has data stored with the usage as COMP-3. INPUT PIC Clause Value passed PIC Clause S9(09) X(11) S9(09) Comp3 9(09) OUTPUT Value Received (Display) 12345678I 123456789 123456789 123456789 OBSERVATION

S9(09) COMP3

? ü ü ü

123456789

Points to be noted:

Case 1: In this case, it is the display which is not showing the correct value but actually correct value is stored internally. In the decimal numeric type ‘Sign’ is always associated with the last byte. If the value stored in S9 (09) is moved to X (09), it will show the correct value during display as well i.e. 123456789. COMP-3 (packed decimal) items occupy one byte storage for every two decimal digits and the rightmost byte contains only one digit and sign i.e. Value +0 +1 +12 +123 +1234 -1 -1234 Comp-3, hex

1234

0C 1C 01 2C 12 3C 01 23 4C 1D 01 23 4D 01 23 4F

Where, ‘C’ stands for positive value, ‘D’ stands for negative value and ‘F’ stands for unsigned integer.

In this case we can show the byte by byte transfer as:

12 34 56 78 9F (Internally stored)

S9 (09) COMP-3

12 34 56 78 format)

I

S9 (09)

(Display