Variante 1 (FIELD-SYMBOLS, CASTING: Struktur <-> Typ x)
* einfachen Typ deklarieren
TYPES: BEGIN OF ty_struct,
matnr TYPE matnr,
int TYPE i,
float TYPE f,
text TYPE char10,
END OF ty_struct.
* Feldsymbol für Binärdaten
FIELD-SYMBOLS <x> TYPE x.
* Feldsymbol für Strukturdaten
FIELD-SYMBOLS <s> TYPE ty_struct.
* Struktur anlegen, deren Daten kovertiert werden soll
DATA(lv_s) = VALUE ty_struct( matnr = '1234567890'
int = 1
float = '1.1'
text = 'abc' ).
* Struktur in Binärdaten umwandeln
ASSIGN lv_s TO <x> CASTING.
IF <x> IS ASSIGNED.
* Binärdaten in Struktur umwandeln
ASSIGN <x> TO <s> CASTING.
IF <s> IS ASSIGNED.
DATA(lv_s2) = <s>.
* Datenausgabe
WRITE: / lv_s2-matnr.
WRITE: / lv_s2-int.
WRITE: / lv_s2-float.
WRITE: / lv_s2-text.
ENDIF.
ENDIF.
Variante 2 (xstring -> Struktur)
* https://www.consolut.com/s/sap-ides-zugriff/d/e/doc/M-CL_ABAP_CONV_IN_CE/
* einfachen Typ deklarieren
TYPES: BEGIN OF ty_struct,
text TYPE char5,
int TYPE i,
END OF ty_struct.
* Zielstruktur
DATA(lv_s) = VALUE ty_struct( ).
* Konverterobjekt mit UTF-8 und Little Endian Konvertierung
DATA(o_conv) = cl_abap_conv_in_ce=>create( encoding = 'UTF-8' endian = 'L' ).
* View-Objekt
DATA(o_view) = cl_abap_view_offlen=>create_legacy_view( lv_s ).
* Eingabepuffer mit Binärdaten
DATA(lv_buffer) = CONV xstring( '616263202000000005000000' ).
* Konvertierung xstring -> Struct
* in: HEX: 616263202000000005000000
* out: Struct: text: abc
* ínt: 5
o_conv->convert_struc( EXPORTING
input = lv_buffer
view = o_view
IMPORTING
data = lv_s ).
* Datenausgabe
WRITE: / lv_s-text.
WRITE: / lv_s-int.
Variante 3 (Struktur -> xstring)
* https://www.consolut.com/s/sap-ides-zugriff/d/e/doc/M-CL_ABAP_CONV_OUT_CE/
* einfachen Typ deklarieren
TYPES: BEGIN OF ty_struct,
text TYPE char5,
int TYPE i,
END OF ty_struct.
* Struktur mit Quelldaten
DATA(lv_s) = VALUE ty_struct( text = 'abc'
int = 5 ).
* Konverterobjekt mit UTF-8 und Little Endian Konvertierung
DATA(o_conv) = cl_abap_conv_out_ce=>create( encoding = 'UTF-8' endian = 'L' ).
* View-Objekt
DATA(o_view) = cl_abap_view_offlen=>create_legacy_view( lv_s ).
* Ausgabepuffer für Binärdaten
DATA: lv_buffer type xstring.
* Konvertierung Struct -> xstring
* in: Struct: text: abc
* ínt: 5
* out: HEX: 616263202000000005000000
o_conv->convert_struc( EXPORTING
data = lv_s
view = o_view
IMPORTING
buffer = lv_buffer ).
* Datenausgabe
WRITE: / lv_buffer.