[ABAP] Binärdaten (HEX) in eine lokale Datei speichern

* HEX-Daten (Beispiel)
DATA(it_solix) = VALUE solix_tab(
                   ( line = '7639BD' )
                   ( line = '67326F' )
                 ).

TRY.
    DATA: lv_action TYPE i.
    DATA: lv_filename TYPE string.
    DATA: lv_fullpath TYPE string.
    DATA: lv_path TYPE string.

* Save-Dialog
    cl_gui_frontend_services=>file_save_dialog( EXPORTING
                                                  default_file_name = 'Data.bin'
                                                  default_extension = 'bin'
                                                  file_filter       = |Binärdaten (*.bin)\|*.bin\|{ cl_gui_frontend_services=>filetype_all }|
                                                CHANGING
                                                  filename          = lv_filename
                                                  path              = lv_path
                                                  fullpath          = lv_fullpath
                                                  user_action       = lv_action ).

    IF lv_action EQ cl_gui_frontend_services=>action_ok.
* XSTRING für Längenbestimung erzeugen
      DATA(lv_xstring) = cl_bcs_convert=>solix_to_xstring( it_solix ).

* Datei lokal speichern
      cl_gui_frontend_services=>gui_download( EXPORTING
                                                filename     = lv_fullpath
                                                filetype     = 'BIN'
                                                bin_filesize = xstrlen( lv_xstring )
                                              CHANGING
                                                data_tab     = it_solix ).

    ENDIF.
  CATCH cx_root INTO DATA(e_text).
    MESSAGE e_text->get_text( ) TYPE 'I'.
ENDTRY.

[ABAP] Variablen vom Typ XSTRING zusammenfügen (addieren)

Variante 1 (einzelne XSTRINGS)

* HEX-Daten (Beispiel)
DATA(lv_xstring) = CONV xstring( 'A1B1C1' ).
DATA(lv_xstring2) = CONV xstring( 'A2B2C2' ).

* XSTRING + XSTRING
CONCATENATE lv_xstring lv_xstring2 INTO lv_xstring IN BYTE MODE.

* Testausgabe
WRITE: lv_xstring.

Variante 2 (eine Tabelle mit XSTRINGS)

TYPES: ty_it_hex TYPE STANDARD TABLE OF xstring WITH DEFAULT KEY.

* Tabelle mit binären Beispieldaten
DATA(it_hex_data) = VALUE ty_it_hex(
                                     ( CONV xstring( 'AABBCC' ) )
                                     ( CONV xstring( 'DDEEFF' ) )
                                     ( CONV xstring( '001122' ) )
                                     ( CONV xstring( '334455' ) )
                                     ( CONV xstring( '667788' ) )
                                   ).

* Zeilen der Tabelle zu einem XSTRING zusammenfügen
CONCATENATE LINES OF it_hex_data INTO DATA(lv_xstring) IN BYTE MODE.

* Testausgabe
WRITE: lv_xstring.

[ABAP] Binärdaten: Bits setzen und auslesen, Bitoperationen

* DEMO_DATA_BIT
TRY.
* zwei Bytes
    DATA: lv_xa TYPE x LENGTH 1.
    DATA: lv_xb TYPE x LENGTH 1.

* Bit 1 und 8 setzen
    SET BIT 1 OF lv_xa.
    SET BIT 8 OF lv_xb.

* xstring mit Bit == 1 an der 7. Stelle
    DATA(lv_hex) = bit-set( 7 ).

    cl_demo_output=>write_data( lv_xa ).
    cl_demo_output=>write_data( lv_xb ).
    cl_demo_output=>write_data( lv_hex ).

* Bit 1 lesen
    DATA(lv_bit1) = 0.
    GET BIT 1 OF lv_xa INTO lv_bit1.

    cl_demo_output=>write_data( lv_bit1 ).

* Bit 8 lesen
    DATA(lv_bit8) = 0.
    GET BIT 8 OF lv_xb INTO lv_bit8.

    cl_demo_output=>write_data( lv_bit8 ).

    DATA: lv_and TYPE x LENGTH 1.
    DATA: lv_or TYPE x LENGTH 1.
    DATA: lv_xor TYPE x LENGTH 1.
    DATA: lv_not TYPE x LENGTH 1.

* AND
    lv_and = lv_xa BIT-AND lv_xb.
    cl_demo_output=>write_data( lv_and ).

* OR
    lv_or = lv_xa BIT-OR lv_xb.
    cl_demo_output=>write_data( lv_or ).

* XOR
    lv_xor = lv_xa BIT-XOR lv_xb BIT-XOR lv_hex.
    cl_demo_output=>write_data( lv_xor ).

* NOT
    lv_not = BIT-NOT lv_xb.
    cl_demo_output=>write_data( lv_not ).

* Bitweises lesen
    DATA(lv_bitstring_a) = ||.
    DATA(lv_bitstring_b) = ||.
    DATA(lv_bitstring_hex) = ||.
    DATA(lv_bitstring_and) = ||.
    DATA(lv_bitstring_or) = ||.
    DATA(lv_bitstring_xor) = ||.
    DATA(lv_bitstring_not) = ||.
    DO 8 TIMES.
      DATA(lv_bit) = 0.
      GET BIT sy-index OF lv_xa INTO lv_bit.
      lv_bitstring_a = lv_bitstring_a && lv_bit.
      GET BIT sy-index OF lv_xb INTO lv_bit.
      lv_bitstring_b = lv_bitstring_b && lv_bit.
      GET BIT sy-index OF lv_hex INTO lv_bit.
      lv_bitstring_hex = lv_bitstring_hex && lv_bit.
      GET BIT sy-index OF lv_and INTO lv_bit.
      lv_bitstring_and = lv_bitstring_and && lv_bit.
      GET BIT sy-index OF lv_or INTO lv_bit.
      lv_bitstring_or = lv_bitstring_or && lv_bit.
      GET BIT sy-index OF lv_xor INTO lv_bit.
      lv_bitstring_xor = lv_bitstring_xor && lv_bit.
      GET BIT sy-index OF lv_not INTO lv_bit.
      lv_bitstring_not = lv_bitstring_not && lv_bit.
    ENDDO.

    cl_demo_output=>write_data( lv_bitstring_a ).
    cl_demo_output=>write_data( lv_bitstring_b ).
    cl_demo_output=>write_data( lv_bitstring_hex ).
    cl_demo_output=>write_data( lv_bitstring_and ).
    cl_demo_output=>write_data( lv_bitstring_or ).
    cl_demo_output=>write_data( lv_bitstring_xor ).
    cl_demo_output=>write_data( lv_bitstring_not ).

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

[ABAP] Interne Tabelle als Excel-Datei (*.xlsx) speichern (cl_fdt_xl_spreadsheet)

* Mit Hilfe der Klasse cl_fdt_xl_spreadsheet können über XML-Transformationen
* Daten aus SAP in XLSX-Dateien geschrieben werden
TRY.
* Testdaten lesen
    SELECT * FROM t001 INTO TABLE @DATA(it_t001).

    IF sy-subrc = 0.
* Header erzeugen
      DATA: it_columns TYPE if_fdt_doc_spreadsheet=>t_column.
      DATA: lv_head TYPE t001.
      DATA(o_desc) = CAST cl_abap_structdescr( cl_abap_structdescr=>describe_by_data( lv_head ) ).

      LOOP AT o_desc->get_components( ) ASSIGNING FIELD-SYMBOL(<c>).
        IF <c> IS ASSIGNED.
          IF <c>-type->kind = cl_abap_typedescr=>kind_elem.
            APPEND VALUE #( id           = sy-tabix
                            name         = <c>-name
                            display_name = <c>-name
                            is_result    = abap_true
                            type         = <c>-type ) TO it_columns.
          ENDIF.
        ENDIF.
      ENDLOOP.

* itab + header -> XML -> xstring
* Achtung: Speicherintensiv und rel. langsam! Es sollten keine großen Datenmengen verarbeitet werden.
      DATA(lv_bin_data) = cl_fdt_xl_spreadsheet=>if_fdt_doc_spreadsheet~create_document( columns      = it_columns " optional
                                                                                         itab         = REF #( it_t001 )
                                                                                         iv_call_type = if_fdt_doc_spreadsheet=>gc_call_dec_table ).
      IF xstrlen( lv_bin_data ) > 0.
        DATA: lv_action TYPE i.
        DATA: lv_filename TYPE string.
        DATA: lv_fullpath TYPE string.
        DATA: lv_path TYPE string.

* Save-Dialog
        cl_gui_frontend_services=>file_save_dialog( EXPORTING
                                                      default_file_name = 'Excel.xlsx'
                                                      default_extension = 'xlsx'
                                                      file_filter       = |Excel-Datei (*.xlsx)\|*.xlsx\|{ cl_gui_frontend_services=>filetype_all }|
                                                    CHANGING
                                                      filename          = lv_filename
                                                      path              = lv_path
                                                      fullpath          = lv_fullpath
                                                      user_action       = lv_action ).

        IF lv_action EQ cl_gui_frontend_services=>action_ok.
* XSTRING -> SOLIX (RAW)
          DATA(it_raw_data) = cl_bcs_convert=>xstring_to_solix( EXPORTING iv_xstring = lv_bin_data ).

* Datei lokal speichern
          cl_gui_frontend_services=>gui_download( EXPORTING
                                                    filename     = lv_fullpath
                                                    filetype     = 'BIN'
                                                    bin_filesize = xstrlen( lv_bin_data )
                                                  CHANGING
                                                    data_tab     = it_raw_data ).


        ENDIF.
      ENDIF.
    ENDIF.
  CATCH cx_root INTO DATA(e_text).
    MESSAGE e_text->get_text( ) TYPE 'I'.
ENDTRY.

[ABAP] Binärdaten (RAW) mit GZIP komprimieren und Base64 codieren

/iwwrk/cl_mgw_workflow_rt_util

TRY.
* Binärdaten
    DATA: lv_xstr TYPE xstring VALUE '54657374737472696E672066C3BC7220646965204B6F6D7072657373696F6E206D697420475A49502E'.
    DATA: lv_xstr_gzip TYPE xstring.

* GZIP komprimieren
    cl_abap_gzip=>compress_binary( EXPORTING raw_in   = lv_xstr
                                   IMPORTING gzip_out = lv_xstr_gzip ).

* Base64 codieren
    DATA(lv_base64_data) = /iwwrk/cl_mgw_workflow_rt_util=>base64_encode( lv_xstr_gzip ).

    WRITE: / lv_base64_data.

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

SSFC_BASE64_ENCODE

* Binärdaten
DATA: lv_xstr TYPE xstring VALUE '54657374737472696E672066C3BC7220646965204B6F6D7072657373696F6E206D697420475A49502E'.
DATA: lv_xstr_gzip TYPE xstring.

* GZIP komprimieren
cl_abap_gzip=>compress_binary( EXPORTING raw_in   = lv_xstr
                               IMPORTING gzip_out = lv_xstr_gzip ).

DATA(lv_base64_data) = ||.

* Base64 codieren
CALL FUNCTION 'SSFC_BASE64_ENCODE'
  EXPORTING
    bindata                  = lv_xstr_gzip
  IMPORTING
    b64data                  = lv_base64_data
  EXCEPTIONS
    ssf_krn_error            = 1
    ssf_krn_noop             = 2
    ssf_krn_nomemory         = 3
    ssf_krn_opinv            = 4
    ssf_krn_input_data_error = 5
    ssf_krn_invalid_par      = 6
    ssf_krn_invalid_parlen   = 7
    OTHERS                   = 8.
IF sy-subrc = 0.
  WRITE: / lv_base64_data.
ENDIF.

[ABAP] Base64 codierten String in Binärdaten (RAW) konvertieren und mit GZIP dekomprimieren

/iwwrk/cl_mgw_workflow_rt_util

TRY.
* Base64 String welcher GZIP-komprimierte Binärdaten beinhaltet
    DATA(lv_base64_data) = |C0ktLikuKcrMS1dIO7ynSCElM1XBOz+3oCi1uDgzP08hN7NEwT3KM0APAA==|.

* Base64 decodieren
    DATA(lv_xstr_gzip) = /iwwrk/cl_mgw_workflow_rt_util=>base64_decode( lv_base64_data ).

* Binärdaten
    DATA: lv_xstr TYPE xstring.
    cl_abap_gzip=>decompress_binary( EXPORTING gzip_in = lv_xstr_gzip IMPORTING raw_out = lv_xstr ).

    DATA: lv_str TYPE string.

* xstring (binary) -> string (UTF-8)
    DATA(o_conv) = cl_abap_conv_in_ce=>create( input    = lv_xstr
                                               encoding = 'UTF-8' ).
    o_conv->read( IMPORTING data = lv_str ).

    WRITE: / lv_str.

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

SSFC_BASE64_DECODE

* Base64 String welcher GZIP-komprimierte Binärdaten beinhaltet
DATA(lv_base64_data) = |C0ktLikuKcrMS1dIO7ynSCElM1XBOz+3oCi1uDgzP08hN7NEwT3KM0APAA==|.

* Binärdaten
DATA: lv_xstr TYPE xstring.
DATA: lv_xstr_gzip TYPE xstring.

* Base64 decodieren
CALL FUNCTION 'SSFC_BASE64_DECODE'
  EXPORTING
    b64data                  = lv_base64_data
  IMPORTING
    bindata                  = lv_xstr_gzip
  EXCEPTIONS
    ssf_krn_error            = 1
    ssf_krn_noop             = 2
    ssf_krn_nomemory         = 3
    ssf_krn_opinv            = 4
    ssf_krn_input_data_error = 5
    ssf_krn_invalid_par      = 6
    ssf_krn_invalid_parlen   = 7
    OTHERS                   = 8.

IF sy-subrc = 0.
  cl_abap_gzip=>decompress_binary( EXPORTING gzip_in = lv_xstr_gzip IMPORTING raw_out = lv_xstr ).

  DATA: lv_str TYPE string.

* xstring (binary) -> string (UTF-8)
  DATA(o_conv) = cl_abap_conv_in_ce=>create( input    = lv_xstr
                                             encoding = 'UTF-8' ).
  o_conv->read( IMPORTING data = lv_str ).

  WRITE: / lv_str.
ENDIF.

[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'
                                                bin_filesize = xstrlen( lv_img_conv_data )
                                              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.