[ABAP] Konvertierung einer Struktur in Binärdaten (x, xstring) und zurück

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.

[ABAP] Konvertierung Integer (ASCII) HEX CHAR

Variante 1 (Integer (ASCII) -> HEX -> Char)

DATA: ascii_hex TYPE x.
DATA: char TYPE c.

DO 26 TIMES.
* INT -> HEX
  ascii_hex = 64 + sy-index.

* HEX -> CHAR
  cl_abap_conv_in_ce=>create( encoding = 'UTF-8' )->convert( EXPORTING input = ascii_hex
                                                             IMPORTING data  = char ).

  WRITE: / sy-index, '->', ascii_hex, '->', char.
ENDDO.

Variante 2 (Char -> Hex -> Integer (ASCII))

DATA: char TYPE c VALUE 'A'.
DATA: ascii_hex TYPE xstring.
DATA: ascii_int TYPE i.

DATA(o_conv) = cl_abap_conv_out_ce=>create( encoding = 'UTF-8' endian = 'L' ).
o_conv->write( data = char ).

* CHAR -> HEX
ascii_hex = o_conv->get_buffer( ).

* HEX -> INT
ascii_int = ascii_hex.

WRITE: / char, '->', ascii_hex, '->', ascii_int.

[ABAP] string (Text) nach xstring (Bytes) wandeln

Variante 1 (cl_abap_conv_out_ce)

DATA: lv_str TYPE string VALUE 'Teststring.'.
DATA: lv_xstr TYPE xstring.

* string (UTF-8) -> xstring (binary)
DATA(o_conv_w) = cl_abap_conv_out_ce=>create( encoding = 'UTF-8' ).
o_conv_w->write( data = lv_str ).
lv_xstr = o_conv_w->get_buffer( ).

WRITE: / lv_xstr.

Variante 2 (SCMS_STRING_TO_XSTRING)

DATA: lv_str TYPE string VALUE 'Teststring.'.
DATA: lv_xstr TYPE xstring.
  
CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
  EXPORTING
    text   = lv_str
  IMPORTING
    buffer = lv_xstr.

WRITE: / lv_xstr.

Variante 3 (cl_proxy_service)

DATA: lv_str TYPE string VALUE 'Teststring.'.
* string (4110 / UTF-8) -> xstring (binary)
DATA(lv_xstr_res) = cl_proxy_service=>cstring2xstring( lv_str ).
WRITE: / lv_xstr_res.

Variante 4 (cl_abap_codepage)

DATA: lv_str TYPE string VALUE 'Teststring.'.
* string -> xstring (binary) (UTF-8)
DATA(xstr) = cl_abap_codepage=>convert_to( source   = lv_str
                                           codepage = 'UTF-8' ).
WRITE: / xstr.

Variante 5 (cl_abap_message_digest)

DATA: lv_str TYPE string VALUE 'Teststring.'.
* string (4110 / UTF-8) -> xstring (binary)
DATA(lv_xstr) = cl_abap_message_digest=>string_to_xstring( lv_str ).

WRITE: / lv_xstr.

Variante 6 (cl_abap_hmac)

DATA: lv_str TYPE string VALUE 'Teststring.'.
* string (4110 / UTF-8) -> xstring (binary)
DATA(lv_xstr) = cl_abap_hmac=>string_to_xstring( lv_str ).

WRITE: / lv_xstr.

Variante 7 (cl_clb_tools)

DATA: lv_str TYPE string VALUE 'Teststring.'.
* string -> xstring (binary) (UTF-8)
DATA(lv_xstr) = cl_clb_tools=>string_to_xstring( lv_str ).

WRITE: / lv_xstr.