释疑の语法Unpack&pack

来源:互联网 发布:地下城与勇士内存优化 编辑:程序博客网 时间:2024/05/17 23:40

For example:  与外围系统接口,外围系统数据库存储数据是有0000的,但是SAP数据库中存储是没有0000,可以用前导零函数或者这个关键字处理

PACK格式:PACK source TO destination.

标准教材上说: In contrast to the conversion rules for elementary data types, a decimal separator in source is ignored.说什么小数点会被忽略,反正我是不信,尝试一下。

DATALV_PACK TYPE P LENGTH DECIMALS VALUE '123.456',
      CHAR1   TYPE LENGTH 10,
      CHAR2   TYPE LENGTH 10.
PACK LV_PACK TO CHAR1.
WRITE / CHAR1.

输出:


果然如此,我居然无言以对。

再举一个例子。

DATALV_PACK TYPE CHAR255 VALUE '王健林.王思聪',
      CHAR1   TYPE LENGTH 10,
      CHAR2   TYPE LENGTH 10.

PACK LV_PACK TO CHAR1.
WRITE / CHAR1.

输出:


原因在标准教材上也有说明:The data type of source must be character-type,flat, and its content must be interpretable as a numeric value. 

再说说比较有用的UNPACK格式:UNPACK source TO destination. 

标准教材上这样说:

If the data type of source is not of the type p with length 16 and without decimal places, then the content of source is converted to this data type. Contrary to the rules described in conversion rules for elementary data types, any decimal separator in source is completely ignored. 
The digits of the interim result are assigned to data object destination right-aligned and without +/- sign. Any additional places in destination are filled with leading zeros. If the length of destination is not sufficient, the assigned variable is truncated from the left. 

大概意思同上面的PACK,唯一的好处是什么,就是刚开始说明的,比如别的系统是需要千导0的,我们得给他按位数转换上。

直接看例子:

DATAPACK  TYPE P LENGTH DECIMALS VALUE '123.456',
      CHAR1 TYPE LENGTH 10,
      CHAR2 TYPE LENGTH 10.
MOVE   PACK TO CHAR1.
UNPACK PACK TO CHAR2.

WRITE / CHAR1.
WRITE / CHAR2.

输出:


1 0
原创粉丝点击