Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Processing Internal Data →  Assignments →  Assigning References →  Setting Field Symbols →  ASSIGN →  ASSIGN - casting_spec →  Casting Examples 

Field Symbols, Casting Built-In Data Types

This example demonstrates how a casting is performed on built-in data types.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA txt(8) TYPE c VALUE '20050606'.

    DATA mytype(1) VALUE 'X'.

    FIELD-SYMBOLS <fs> TYPE ANY.

    DATA(out) = cl_demo_output=>new(
      )->begin_section( 'Cast with built-in types' ).

    ASSIGN txt TO <fs>.
    out->write( |<fs> with inherited type c: { <fs> }| ).

<span class="blue">* correct -------------------------------------------------------------</span>

    ASSIGN txt TO <fs> CASTING TYPE i.
    out->write( |<fs> casted with i: { <fs> }| ).

    ASSIGN txt TO <fs> CASTING TYPE (mytype).
    out->write( |<fs> casted with x: { <fs> }| ).

    out->display( ).

<span class="blue">* obsolete, not allowed in methods ------------------------------------</span>

    "ASSIGN txt TO <fs> TYPE 'D'.

    "ASSIGN txt TO <fs> TYPE mytype.

Description

This example demonstrates how casting works on built-in data types. First the character string txt is assigned to the field symbol <fs> without casting. Afterwards, txt is assigned to <fs> using casting on types i and x. The second output value depends on the byte order of the current platform. The paired numbers in the last output row represent the hexadecimal code for the character in txt and depend on the character representation on the current AS Instance.

The section of the method that has been commented out also shows the syntax for the relevant obsolete casting.