[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] Mit Hilfe der Klasse cl_fxs_image_processor Infos zu einem Bild auslesen, Größe verändern (Resize) und Bildformat konvertieren (Convert)

* Im Beispiel wird das Eingabebild (PNG) nach JPG konvertiert und auf 800x600px in der Größe verändert

* Dateiname der Bilddatei zum hochladen
PARAMETERS: p_fname TYPE file_table-filename OBLIGATORY.
PARAMETERS: p_width TYPE i DEFAULT 800.
PARAMETERS: p_height TYPE i DEFAULT 600.

* wenn die F4-Hilfe für den Dateinamen aufgerufen wird
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_fname.

  DATA: lv_rc TYPE i.
  DATA: it_files TYPE filetable.
  DATA: lv_action TYPE i.

* File-Tabelle leeren, da hier noch alte Einträge von vorherigen Aufrufen drin stehen können
  CLEAR it_files.

* FileOpen-Dialog aufrufen
  TRY.
      cl_gui_frontend_services=>file_open_dialog( EXPORTING
                                                    file_filter = |png (*.png)\|*.png\|{ cl_gui_frontend_services=>filetype_all }|
                                                  CHANGING
                                                    file_table  = it_files
                                                    rc          = lv_rc
                                                    user_action = lv_action ).

      IF lv_action = cl_gui_frontend_services=>action_ok.
* wenn Datei ausgewählt wurde
        IF lines( it_files ) > 0.
* ersten Tabelleneintrag lesen
          p_fname = it_files[ 1 ]-filename.
        ENDIF.
      ENDIF.

    CATCH cx_root INTO DATA(e_text).
      MESSAGE e_text->get_text( ) TYPE 'S' DISPLAY LIKE 'E'.
  ENDTRY.

START-OF-SELECTION.

  TRY.

  DATA: lv_filesize TYPE w3param-cont_len.
  DATA: lv_filetype TYPE w3param-cont_type.
  DATA: it_bin_data TYPE w3mimetabtype.

  WRITE: / p_fname.

* Bild auf Appl. Server hochladen (binary)
  cl_gui_frontend_services=>gui_upload( EXPORTING
                                          filename   = |{ p_fname }|
                                          filetype   = 'BIN'
                                        IMPORTING
                                          filelength = lv_filesize
                                        CHANGING
                                          data_tab   = it_bin_data ).

* solix -> xstring
  DATA(lv_img_data) = cl_bcs_convert=>solix_to_xstring( it_solix = it_bin_data ).

* FXS-Objekt holen
      DATA(o_ip) = NEW cl_fxs_image_processor( ).

* Bild zum Konverter hinzufügen
      DATA(lv_hndl) = o_ip->add_image( iv_data = lv_img_data ).

      DATA: lv_mimetype	TYPE string.
      DATA: lv_xres	TYPE i.
      DATA: lv_yres	TYPE i.
      DATA: lv_xdpi	TYPE i.
      DATA: lv_ydpi	TYPE i.
      DATA: lv_bitdepth	TYPE i.

* Infos holen
      o_ip->get_info( EXPORTING
                        iv_handle   = lv_hndl
                      IMPORTING
                        ev_mimetype = lv_mimetype
                        ev_xres     = lv_xres
                        ev_yres     = lv_yres
                        ev_xdpi     = lv_xdpi
                        ev_ydpi     = lv_ydpi
                        ev_bitdepth = lv_bitdepth ).

* Infos ausgeben
      WRITE: / 'MIME-Type:', lv_mimetype.
      WRITE: / '        X:', lv_xres.
      WRITE: / '        Y:', lv_yres.
      WRITE: / '    X-DPI:', lv_xdpi.
      WRITE: / '    Y-DPI:', lv_ydpi.
      WRITE: / '      Bit:', lv_bitdepth.

* Größe ändern (Resize)
      o_ip->resize( EXPORTING
                      iv_handle = lv_hndl
                      iv_xres   = p_width
                      iv_yres   = p_height ).

* Konvertierung (Convert) nach JPG
* Zielformate können nur sein (andernfalls gibt es eine Exception vom Typ cx_sy_range_out_of_bounds):
*   cl_fxs_mime_types=>co_image_bitmap
*   cl_fxs_mime_types=>co_image_png
*   cl_fxs_mime_types=>co_image_jpeg
      o_ip->convert( EXPORTING
                      iv_handle = lv_hndl
                      iv_format = cl_fxs_mime_types=>co_image_jpeg ).

* Infos holen
      o_ip->get_info( EXPORTING
                        iv_handle   = lv_hndl
                      IMPORTING
                        ev_mimetype = lv_mimetype
                        ev_xres     = lv_xres
                        ev_yres     = lv_yres
                        ev_xdpi     = lv_xdpi
                        ev_ydpi     = lv_ydpi
                        ev_bitdepth = lv_bitdepth ).

* Infos ausgeben
      WRITE: / 'MIME-Type:', lv_mimetype.
      WRITE: / '        X:', lv_xres.
      WRITE: / '        Y:', lv_yres.
      WRITE: / '    X-DPI:', lv_xdpi.
      WRITE: / '    Y-DPI:', lv_ydpi.
      WRITE: / '      Bit:', lv_bitdepth.

* Binärdaten (xstring) des Bildes holen
      DATA(lv_img_conv_data) = o_ip->get_image( EXPORTING iv_handle = lv_hndl ).

* xstring -> solix
      DATA(it_img_conv_data) = cl_bcs_convert=>xstring_to_solix( iv_xstring = lv_img_conv_data ).

      WRITE: / |{ p_fname }.jpg|.

* Image lokal speichern
      cl_gui_frontend_services=>gui_download( EXPORTING
                                                filename = |{ p_fname }.jpg|
                                                filetype = 'BIN'
                                              CHANGING
                                                data_tab = it_img_conv_data ).

    CATCH cx_root INTO DATA(e_txt).
      WRITE: / e_txt->get_text( ).
  ENDTRY.

[ABAP] Datei binär auf den Applikationsserver schreiben

Variante 1 (xstring)

* Dateiinhalt (Binärdatenstrom) als xstring
DATA(lv_bytes_xstr) = CONV xstring( 'A0B0C0' ).
* Pfad + Dateiname auf dem Appl.-Server
DATA(lv_file) = '/usr/sap/tmp/file.bin'.

TRY.
* Datei zum binären Schreiben öffnen
    OPEN DATASET lv_file FOR OUTPUT IN BINARY MODE.
    IF sy-subrc = 0.

* Binärdaten in die Datei schreiben
      TRANSFER lv_bytes_xstr TO lv_file.

* Datei schließen
      CLOSE DATASET lv_file.
    ELSE.
      WRITE: / 'Konnte Datei nicht öffnen:', lv_file.
    ENDIF.

  CATCH cx_root INTO DATA(e_txt).
    WRITE: / e_txt->get_text( ).
ENDTRY.

Variante 2 (itab mit Zeilen von 1024Bytes Länge)

TYPES: ty_raw1024 TYPE x LENGTH 1024.

* itab mit Zeilen von je 1kB Daten
DATA: it_raw1024 TYPE STANDARD TABLE OF ty_raw1024 WITH DEFAULT KEY.

* 2kB Testdaten erzeugen
it_raw1024 = VALUE #(
                      ( CONV xstring( 'A0B0C0FFFF' ) )
                      ( CONV xstring( 'C1D2E3FFFF' ) )
                    ).

* Pfad + Dateiname auf dem Appl.-Server
DATA(lv_file) = '/usr/sap/tmp/file.bin'.

TRY.
* Datei zum binären Schreiben öffnen
    OPEN DATASET lv_file FOR OUTPUT IN BINARY MODE.
    IF sy-subrc = 0.

* Binärdaten Zeilenweise in die Datei schreiben
* es wird immer die volle Zeilenlänge geschrieben
      LOOP AT it_raw1024 ASSIGNING FIELD-SYMBOL(<x>).
        TRANSFER <x> TO lv_file.
      ENDLOOP.

* Datei schließen
      CLOSE DATASET lv_file.
    ELSE.
      WRITE: / 'Konnte Datei nicht öffnen:', lv_file.
    ENDIF.

  CATCH cx_root INTO DATA(e_txt).
    WRITE: / e_txt->get_text( ).
ENDTRY.

[ABAP] Binärdaten (xstring) zu String wandeln (xstring -> string)

Variante 1 (xstring -> string)

* Binärdaten als xstring
DATA(lv_xstring) = CONV xstring( 'A0B0C0FF' ).
* xstring -> string
DATA(lv_string) = CONV string( lv_xstring ).

WRITE: / lv_string.

Variante 2 (String als formatierten Hexcode ausgeben)

DATA: lv_mime_type TYPE string.
DATA: lv_image_bytes TYPE xstring.
DATA: it_text80 TYPE STANDARD TABLE OF text80 WITH DEFAULT KEY.

TRY.
* Ein paar Bytes holen: Screenshot aufnehmen
    cl_gui_frontend_services=>get_screenshot( IMPORTING
                                                mime_type_str = lv_mime_type
                                                image         = lv_image_bytes ).

* xstring -> string
    DATA(lv_string) = CONV string( lv_image_bytes ).

* formatierte Ausgabe (80 Zeichen Breite)
    CALL FUNCTION 'SWA_STRING_TO_TABLE'
      EXPORTING
        character_string = lv_string
      IMPORTING
        character_table  = it_text80.

    LOOP AT it_text80 ASSIGNING FIELD-SYMBOL(<t>).
      WRITE: / <t>.
    ENDLOOP.

  CATCH cx_root INTO DATA(e_txt).
ENDTRY.

[ABAP] MIME-Objekt aus dem Repository laden

* Objekt-ID des Mime-Objekts
DATA: lv_object_id TYPE char32 VALUE 'EE3ECB43B3E70B43E10000000A15509A'.

DATA: lv_desc TYPE skwf_desc.
DATA: lv_docname TYPE skwf_urlp.
DATA: lv_filename TYPE string.
DATA: lv_filesize TYPE i.
DATA: lv_mimetype TYPE mr_mimtype.
DATA: lv_langu TYPE sy-langu.

DATA: it_bin_data TYPE sdokcntbins. " RAW1022

DATA(lv_io) = VALUE skwf_io( objtype = 'L' " Unbestimmt (= alle), F Verzeichnis, L Loio, P Phio, R Relation
                             class = 'M_IMAGE_L'
                             objid = lv_object_id ).

* Sprachvorgabe
lv_langu = 'D'.

* MIME-Objekt laden
cl_wb_mime_repository=>load_mime( EXPORTING
                                    io              = lv_io
                                  IMPORTING
                                    docname         = lv_docname
                                    description     = lv_desc
                                    filename        = lv_filename
                                    filesize        = lv_filesize
                                    bin_data        = it_bin_data
                                    mimetype        = lv_mimetype
                                  CHANGING
                                    language        = lv_langu ).

WRITE: / lv_docname.
WRITE: / lv_desc.
WRITE: / lv_filename.
WRITE: / lv_filesize.
WRITE: / lv_mimetype.
WRITE: / lv_langu.

DATA: lv_xstr_line TYPE xstring.
DATA: lv_xstr_result TYPE xstring.

* RAW (binary) -> xstring
CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
  EXPORTING
    input_length = lv_filesize
  IMPORTING
    buffer       = lv_xstr_result
  TABLES
    binary_tab   = it_bin_data
  EXCEPTIONS
    failed       = 1
    OTHERS       = 2.

IF sy-subrc NE 0.
  MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

[ABAP] xstring nach RAW (binary) wandeln

Variante 1 (SCMS_XSTRING_TO_BINARY)

DATA: lv_xstr TYPE xstring VALUE 'A0B0C0'.
DATA: it_bin_data TYPE STANDARD TABLE OF raw255.

* xstring -> RAW (binary)
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
  EXPORTING
    buffer     = lv_xstr
  TABLES
    binary_tab = it_bin_data.

Variante 2 (cl_swf_utl_convert_xstring)

DATA: lv_xstr TYPE xstring VALUE 'A0B0C0'.
DATA: it_bin_data TYPE STANDARD TABLE OF raw255.

* xstring -> RAW (binary)
cl_swf_utl_convert_xstring=>xstring_to_table( EXPORTING
                                                i_stream = lv_xstr
                                              IMPORTING
                                                e_table  = it_bin_data ).

Variante 3 (cl_scp_change_db)

DATA: lv_size TYPE i.
DATA: lv_xstr TYPE xstring VALUE 'A0B0C0'.
DATA: it_bin_data TYPE STANDARD TABLE OF raw255.

* XString (Bytes) -> RAW (iTab)
cl_scp_change_db=>xstr_to_xtab( EXPORTING
                                  im_xstring = lv_xstr
                                IMPORTING
                                  ex_size    = lv_size
                                  ex_xtab    = it_bin_data ).

Variante 4 (cl_bcs_convert)

DATA: lv_xstr TYPE xstring VALUE 'A0B0C0'.

DATA(it_raw_data) = cl_bcs_convert=>xstring_to_solix( EXPORTING iv_xstring = lv_xstr ).

[ABAP] RAW (binary) nach xstring wandeln

Variante 1 (SCMS_BINARY_TO_XSTRING)

DATA: it_bin_data TYPE STANDARD TABLE OF raw255. " iTab mit Binärdaten
DATA: lv_bytes TYPE i.                           " reale Länge der Daten in Bytes
DATA: lv_xstr_result TYPE xstring.

...

CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
  EXPORTING
    input_length = lv_bytes
  IMPORTING
    buffer       = lv_xstr_result
  TABLES
    binary_tab   = it_bin_data
  EXCEPTIONS
    failed       = 1
    OTHERS       = 2.

IF sy-subrc NE 0.
  MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

Variante 2 (cl_swf_utl_convert_xstring)

DATA: lv_size TYPE i.
DATA: it_raw_data TYPE STANDARD TABLE OF raw255.

cl_gui_frontend_services=>gui_upload( EXPORTING
                                        filename     = 'c:\temp\binaryfile.dat'
                                        filetype     = 'BIN'
                                        read_by_line = space
                                      IMPORTING
                                        filelength   = lv_size
                                      CHANGING
                                        data_tab     = it_raw_data ).

DATA(lv_xlsx_str) = cl_swf_utl_convert_xstring=>table_to_xstring( i_table = it_raw_data
                                                                  i_size  = lv_size ).

Variante 3 (cl_scp_change_db)

DATA: lv_size TYPE i.
DATA: it_raw_data TYPE STANDARD TABLE OF raw255.
DATA: lv_xstr_result TYPE xstring.

cl_gui_frontend_services=>gui_upload( EXPORTING
                                        filename     = 'c:\temp\binaryfile.dat'
                                        filetype     = 'BIN'
                                        read_by_line = space
                                      IMPORTING
                                        filelength   = lv_size
                                      CHANGING
                                        data_tab     = it_raw_data ).
                                        
cl_scp_change_db=>xtab_to_xstr( EXPORTING
                                  im_xtab    = it_raw_data
                                  im_size    = lv_size
                                IMPORTING
                                  ex_xstring = lv_xstr_result ).

Variante 4 (cl_bcs_convert)

* solix -> xstring
DATA: lv_size TYPE i.
DATA: it_raw_data TYPE solix_tab.

cl_gui_frontend_services=>gui_upload( EXPORTING
                                        filename     = 'c:\temp\binaryfile.dat'
                                        filetype     = 'BIN'
                                        read_by_line = space
                                      IMPORTING
                                        filelength   = lv_size
                                      CHANGING
                                        data_tab     = it_raw_data ).

DATA(lv_xstr_result) = cl_bcs_convert=>solix_to_xstring( it_solix = it_raw_data ).

Variante 5 (cl_abap_conv_obj)

DATA: it_bin_data TYPE raw255. " iTab mit Binärdaten
DATA: lv_bytes TYPE i.         " reale Länge der Daten in Bytes

...

DATA: lv_xstr_line TYPE xstring.
DATA: lv_xstr_result TYPE xstring.

* RAW (binary) -> xstring
DATA(o_conv) = NEW cl_abap_conv_obj( ).

LOOP AT it_bin_data ASSIGNING FIELD-SYMBOL(<l>).
* Zeile konvertieren
  o_conv->convert( EXPORTING
                     inbuff = <l>
                     outbufflg = 65535 " max. Zeilenlänge in it_bin_data
                   IMPORTING
                     outbuff = lv_xstr_line ).

* Zeilen zusammenbauen
  CONCATENATE lv_xstr_result lv_xstr_line INTO lv_xstr_result IN BYTE MODE.
ENDLOOP.

* evtl. Rest von xstring abschneiden, für reale Länge der Daten
lv_xstr_result = lv_xstr_result(lv_bytes).