ABAP Help Document(15):8.数据类型转换

8.Assignments

8.1Assignment rules

数值型数据类型:i(b,s),p,decfloat16,decfloat34,f;

字符型数据类型:c,string,n,d,t;

类:CL_ABAP_DECFLOAT,READ_DECFLOAT34 and READ_DECFLOAT16方法转换字符型数据为浮点数;

字节数据类型:x,xstring;

CL_ABAP_CONV_IN_CE:read binary input stream;

CL_ABAP_CONV_OUT_CE:write binary output stream;

CL_ABAP_CONV_X2X_CE:read input stream,write output stream;

示例:

FORM f_type_trans_cost.
  DATA:lv_i TYPE I VALUE 10.
  DATA:lv_str TYPE string.
  DATA:lv_begin TYPE I.
  DATA:lv_end TYPE I.
  DATA:lv_time TYPE I.
  "获取类型转换花费时间
  GET RUN TIME FIELD lv_begin.
  DO 10 TIMES.
  ENDDO.
  GET RUN TIME FIELD lv_end.
  lv_time = lv_end - lv_begin.
  GET RUN TIME FIELD lv_begin.
  DO 10 TIMES.
    lv_str = lv_i.
  ENDDO.
  GET RUN TIME FIELD lv_end.
  lv_time = lv_end - lv_begin - lv_time.
  WRITE:/ lv_time.
ENDFORM. 

8.2Value Assignments

示例:

"value assignment
*MOVE {source {TO|?TO} destination}
*   | {EXACT source TO destination}.
*destination {=|?=} source.
"TO,= 为向上转型;
"?TO,?= 为向下转型;
*MOVE EXACT source TO destination.
"exactly转换,确保无值的损失,如果转换失败报错
*destination1 = destination2 = ... = destination = source. 
"链式赋值
*MOVE-CORRESPONDING [EXACT] struc1 TO struc2. 
"结构体赋值,复杂结构体结构必须一致
"UNPACK source TO destination. 
"destination必须是字符型数据;
FORM f_value_assign.
  DATA:lv_text     TYPE string VALUE `4 Apples + 3 Oranges`.
  DATA:lv_num_text TYPE n LENGTH 8.
  DATA:lo_exc      TYPE REF TO cx_sy_conversion_error.
  MOVE lv_text TO lv_num_text.
  MESSAGE lv_num_text TYPE 'I'.
  TRY.
      MOVE EXACT lv_text TO lv_num_text.
    CATCH cx_sy_conversion_error INTO lo_exc.
      MESSAGE lo_exc TYPE 'I' DISPLAY LIKE 'E'.
  ENDTRY.
ENDFORM. 

8.3Setting references

示例:

"References,引用
"ASSIGN mem_area TO <fs> casting_spec range_spec.
"mem_area
*  { dobj[+off][(len)] }
*| { dynamic_dobj }
*| { dynamic_access }
"dynamic_dobj
*  { (name) }
*| { dref->* }
*| { dobj INCREMENT inc }
*| { COMPONENT comp OF STRUCTURE struc }
"dynamic_access
*{ cref->(attr_name) }
*  | { iref->(attr_name) }
*  | { (class_name)=>(attr_name) }
*  | { (class_name)=>attr }
*  | { class=>(attr_name) }
"casting_spec
*{ }
*| { CASTING { { }
*| {TYPE type|(name)}
*| {LIKE dobj}
*| {[TYPE p] DECIMALS dec}
*| {TYPE HANDLE handle} } }
*| { obsolete_casting }
"range_spec
*{ } | {RANGE range}.
FORM f_reference.
  DATA:lv_string TYPE string VALUE 'test'.
  DATA:lv_dref TYPE REF TO DATA.
  FIELD-SYMBOLS:<fs_data> TYPE ANY.
  CREATE DATA lv_dref LIKE lv_string.
  ASSIGN lv_dref->* TO <fs_data>.
  <fs_data> = lv_string.
  WRITE:/ <fs_data>.

  "数据类型
  TYPES:BEGIN OF s_struc1,
          name TYPE string,
          age TYPE i,
        END OF s_struc1.
  DATA:lv_dtype TYPE C LENGTH 1.
  DATA:lt_spfli TYPE TABLE OF spfli.
  DATA:ls_struc1 TYPE s_struc1.
  DATA:lv_data TYPE I.
  FIELD-SYMBOLS:<fs_comp> TYPE ANY.
  "获取数据类型,table类型返回h;
  "structure类型返回v;
  "单个参数,返回数据类型;
  DESCRIBE FIELD ls_struc1 TYPE lv_dtype.
  IF lv_dtype <> 'u' AND lv_dtype <> 'v'.
    RETURN.
  ENDIF.
  DO.
    ASSIGN COMPONENT sy-index OF STRUCTURE ls_struc1 TO <fs_comp>.
    IF sy-subrc <> 0.
      EXIT.
    ELSE.
      WRITE:/ <fs_comp>.
    ENDIF.
  ENDDO.

  "获取结构体
  DATA:lo_struc_desc TYPE REF TO cl_abap_structdescr.
  FIELD-SYMBOLS:<fs_comp_desc> LIKE LINE OF cl_abap_structdescr=>components.
  lo_struc_desc ?= cl_abap_typedescr=>describe_by_data( ls_struc1 ).
  LOOP AT lo_struc_desc->components ASSIGNING <fs_comp_desc>.
    ASSIGN COMPONENT <fs_comp_desc>-name OF STRUCTURE lt_spfli TO <fs_comp>.
    WRITE:/ <fs_comp>.
  ENDLOOP.
ENDFORM. 

示例2:

"casting类型转换
FORM f_reference_casting.
  TYPES:BEGIN OF s_date,
         year TYPE C LENGTH 4,
         month TYPE C LENGTH 2,
         day TYPE C LENGTH 2,
       END OF s_date.
  FIELD-SYMBOLS:<fs_date> TYPE s_date.
  FIELD-SYMBOLS:<fs_data> TYPE ANY.
  FIELD-SYMBOLS:<fs_n> TYPE N.
  "casting类型转换
  ASSIGN sy-datum TO <fs_date> CASTING.
  WRITE:/ <fs_date>-year,<fs_date>-month,<fs_date>-day.

  "casting decimal
  DATA:lv_p1 TYPE P DECIMALS 2 VALUE '12345.67'.
  DATA:lv_p2 TYPE P DECIMALS 2.
  FIELD-SYMBOLS:<fs_any1> TYPE ANY.
  FIELD-SYMBOLS:<fs_any2> TYPE ANY.
  "1位小数
  ASSIGN lv_p1 TO <fs_any1> CASTING TYPE P DECIMALS 1.
  WRITE:/ <fs_any1>.
  "2位小数
  lv_p2 = <fs_any1>.
  WRITE:/ lv_p2.
  "三位小数
  ASSIGN lv_p2 TO <fs_any2> CASTING TYPE P DECIMALS 3.
  WRITE:/ <fs_any2>.
ENDFORM. 

示例3:get reference

"get reference
*GET REFERENCE OF dobj INTO dref.
FORM f_get_reference.
  TYPES:c1 TYPE C LENGTH 10.
  DATA:lv_dref TYPE REF TO c1.
  DATA:lt_dref LIKE TABLE OF lv_dref.
  DATA:lv_text TYPE C LENGTH 10.
  DO 10 TIMES.
    lv_text = 'test' && sy-index.
    GET REFERENCE OF lv_text INTO lv_dref.
    APPEND lv_dref TO lt_dref.
  ENDDO.
  LOOP AT lt_dref INTO lv_dref.
   WRITE:/ lv_dref->*.
  ENDLOOP.
ENDFORM.

8.4Initialization

    初始化,clear,free,unassign。

示例:

"初始化
"CLEAR
"如果清空内表及表头行需要加上[],否则只清空表头
*CLEAR dobj [ {WITH val [IN {CHARACTER|BYTE} MODE] } 
*           | {WITH NULL} ]. 
"FREE,如果dobj是内表,清空table,不影响head line
*FREE dobj. 
"初始化field symbol
*UNASSIGN <fs>. 
上一篇:搬家第五天-120.Wincc V7.3 ListView控件初步使用12-删除选定的行


下一篇:ABAP Help Document(26):11.5 ABAP File Interface