PACKED DECIMAL DATA


A packed decimal field occupies 1 byte for every 2 digits, except for the right-most digit, which shares its byte with the sign. The formula used to calculate the number of bytes occupied by a packed decimal field is a follows:

         bytes = (declared length / 2) + 1 

Consider a 5-position packed decimal field with four decimal positions. Using the above formula to calculate the number of bytes occupied, gives:

        bytes = (5 / 2) + 1 

The number of bytes occupied by this packed decimal field is three. Note that the number of decimal positions (four in this example) has no impact on the bytes occupied.

Figure B.3 shows the storage allocation of a seven-position packed decimal field, with two decimal positions. The value is set to zero.

Byte Position

1

2

3

4

Bits 0–3 (in hex)

0

0

0

0

Bits 4–7 (in hex)

0

0

0

F


Figure B.3: Storage for a seven-position packed decimal field (value 0).

Each number of a decimal value is coded into its binary form when copied to a packed decimal field. The values are copied starting with the sign. Then the numeric digits are copied, starting with the right-most number and continuing to the left.

Figure B.4 shows a seven-position packed decimal field with two decimal positions. The field is set to 1207.38 with a positive sign.

Byte Position

1

2

3

4

Bits 0–3 (in hex)

0

2

7

8

Bits 4–7 (in hex)

1

0

3

F


Figure B.4: Storage for a seven-position packed decimal positive field (value 1207.38).

This spelunking storage technique is the most common format for decimal data on the IBM AS/400. The AS/400 SLIC has been optimized to handle packed decimal data.

Although the sign is stored in bits 4 to 7 of the right-most byte for packed decimal fields, and in bits 0 to 3 of the right-most byte for zoned decimal fields, it is handled basically the same for both.

The sign for a packed decimal field (i.e., bits 4 to 7 of the rightmost byte) must be X'A' to X'F', with X'F' being the default value for a positive sign, and X'D' being the default value for a negative sign. Table B.2 describes the valid signs for a packed decimal field.

Table B.2: Valid Sign Values for Packed Decimal Fields

Sign (in Hex)

Sign (in Binary)

Sign (Symbol)

Description

X'F'

B'1111'

+

All bits are on, the sign is positive (DEFAULT).

X'D'

B'1101'

-

Bit 2 is off, the sign is negative (DEFAULT).

X'A'

B'1010'

+

Bits 1 and 3 are off, the sign is considered positive.

X'B'

B'1011'

+

Bit 1 is off, the sign is considered positive.

X'C'

B'1100'

-

Bits 2 and 3 are off, the sign is negative.

X'E'

B'1110'

+

Bit 3 is off, the sign is considered positive.

Note that the declared length of a packed decimal field is the value that is returned by the %SIZE built-in function. If the field's length is five-positions with two decimal positions, the %SIZE built-in function returns 5 as the field's size.




The Modern RPG IV Language
The Modern RPG IV Language
ISBN: 1583470646
EAN: 2147483647
Year: 2003
Pages: 156
Authors: Robert Cozzi

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net