100% found this document useful (2 votes)
2K views3 pages

Comp-3 To Comp - Decimal Conversions

This document discusses conversions between COMP and COMP-3 packed decimal formats and other numeric data types in COBOL. It provides examples of how values are stored and behave when moved between these different data formats. Key points are that COMP stores the sign in the first byte while COMP-3 stores it in the last byte, and values may be truncated during conversion if the target field is not large enough to accommodate the source value. Intermediate variables can be used to avoid truncation in some cases.

Uploaded by

sxdasgu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
2K views3 pages

Comp-3 To Comp - Decimal Conversions

This document discusses conversions between COMP and COMP-3 packed decimal formats and other numeric data types in COBOL. It provides examples of how values are stored and behave when moved between these different data formats. Key points are that COMP stores the sign in the first byte while COMP-3 stores it in the last byte, and values may be truncated during conversion if the target field is not large enough to accommodate the source value. Intermediate variables can be used to avoid truncation in some cases.

Uploaded by

sxdasgu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 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


stored with the usage as COMP.

INPUT OUTPUT OBSERVATION


PIC Value PIC Value
Claus passed Claus Received
e e (Display)
9(10) 1234567891 ü
9(10)
Comp- 1234567891 ü
3
X(10) 234567891 ?
S9(09) 123456789
123456789
COMP 1 S9(10) ?
A
S9(10)
Comp- 1234567891 ü
3
S9(09) 23456789A ?
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 OUTPUT OBSERVATION


PIC Value PIC Value
Clause passed Clause Received
(Display)
S9(09) 12345678I ?
X(11) 123456789 ü
S9(09)
S9(09)
COMP- 123456789
Comp- 123456789 ü
3
3
9(09) 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 Comp-3, hex


+0 0C
+1 1C
+12 01 2C
+123 12 3C
+1234 01 23 4C
-1 1D
-1234 01 23 4D
1234 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 S9 (09) COMP-3
(Internally stored)

12 34 56 78 I S9 (09) (Display
format)

You might also like