Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

PACK statement

Former Member
0 Likes
4,061

Can anyone brief me the answers for these questions?

1. What is the use of the PACK and UNPACK statement.

2. If i say a character is packed what does it means?

3. If i say a characted is unpacked what does it mean?

Regards,

Phyrose.

6 REPLIES 6
Read only

Former Member
0 Likes
1,880

Hi,

PACK statement converts the content of the data object source to the data type p of length 16 without decimal places.

Unpacks the packed field f and places it in the field g with leading zeros. If g is too short, it is truncated on the left.

Example

DATA: P_FIELD(2) TYPE P VALUE 103,

C_FIELD(4) TYPE C.

UNPACK P_FIELD TO C_FIELD.

WRITE: / C_FIELD.

<b>Reward points</b>

Regards

Read only

0 Likes
1,880

Pack

http://www.sts.tu-harburg.de/teaching/sap_r3/ABAP4/pack.htm

DATA C_FIELD(4) TYPE C VALUE '0103',

P_FIELD(2) TYPE P.

PACK C_FIELD TO P_FIELD.

Unpack

http://www.sts.tu-harburg.de/teaching/sap_r3/ABAP4/unpack.htm

DATA: P_FIELD(2) TYPE P VALUE 103,

C_FIELD(4) TYPE C.

UNPACK P_FIELD TO C_FIELD.

Read only

jayanthi_jayaraman
Active Contributor
0 Likes
1,880

Hi,

Packing means you are deleting the leading zeros.Unpacking means you are adding leading zeros according to character's width.

Read only

Former Member
0 Likes
1,880

Hi,

PACK source TO destination.

This statement, which is forbidden in classes, converts the content of the data object source to the data type p of length 16 without decimal places. Decimal separator in source is ignored.

The data type of source must be character-type, flat, and its content must be interpretable as a numeric value. The data type of destination must be flat. If destination has the data type p, the interim result is assigned to it from left to right. Surplus characters are cut off on the left, and the decimal places are determined by the data type of destination. If destination does not have the data type p, the interim result is converted to the data type of destination according to the rules in the conversion table for source field type p.

The function of the statement PACK is based on the second half-byte of the code corresponding to a character in most character representations of the BCD representation for the corresponding numeric value. This compilation is generally known as "packing".

If the source field contains a number without a decimal separator, and the target field has dobj2 of data type p with sufficient length and without decimal places, the result of the PACK statement (which is forbidden in classes) corresponds to the result of the statement MOVE.

UNPACK f TO g.

Effect

Unpacks the packed field f and places it in the field g with leading zeros. If g is too short, it is truncated on the left.

eg

data: v_matnr like mara-matnr,

c_test(10) value '1234'.

unpack c_test to v_matnr.

v_matnr now contains 000000000000001234 .

Read only

Former Member
0 Likes
1,880

Hi,

PACK

Basic form

PACK f TO g.

Effect

Places the character field f in packed format in the field g . Reverse of the UNPACK command.

Example

DATA C_FIELD(4) TYPE C VALUE '0103',

P_FIELD(2) TYPE P.

PACK C_FIELD TO P_FIELD.

C_FIELD: C'0103' --> P_FIELD: P'103C'

Note

The field f can contain up to 16 characters.

=================================================

UNPACK

Basic form

UNPACK f TO g.

Effect

Unpacks the packed field f and places it in the field g with leading zeros. If g is too short, it is truncated on the left.

Example

DATA: P_FIELD(2) TYPE P VALUE 103,

C_FIELD(4) TYPE C.

UNPACK P_FIELD TO C_FIELD.

P_FIELD: P'103C' --> C_FIELD: C'0103'

Notes

If f is not type P , it is converted to type P (see MOVE ).

g should always be type C . Otherwise, unwanted side effects may occur.

The sign in the packed number is ignored.

Don't forget to reward if useful....

Read only

Former Member
0 Likes
1,880

Hi,

You can use PACK keyword which Converts type C variables to type P.

Syntax

PACK <f> TO <g>.

Packs the string <f> and places it in the field <g>. This can be reversed with the UNPACK statement.

Example:

DATA: PACK TYPE P DECIMALS 2.

PACK = '12345'.

WRITE PACK.

Output:

123.45

Regards,

Padmam.