[ABAP] String als Hexcode (HEX) 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 ).

* Breite 80 Zeichen für Ausgabe
    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 (FuBa)

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_xml_bytes ).

[ABAP] RAW (binary) nach xstring wandeln

Variante 1 (FuBa)

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_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(<fs_bin_line>).
* Zeile konvertieren
  o_conv->convert( EXPORTING
                     inbuff = <fs_bin_line>
                     outbufflg = 65535
                   IMPORTING
                     outbuff = lv_xstr_line ).

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

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

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

DATA: lv_xstr TYPE xstring VALUE '54657374737472696E672066C3BC7220646965204B6F6D7072657373696F6E206D697420475A49502E'.
DATA: lv_str TYPE string.
  
* xstring (binary) -> string (UTF-8)
DATA(o_conv_r) = cl_abap_conv_in_ce=>create( input = lv_xstr encoding = 'UTF-8' ).
o_conv_r->read( IMPORTING data = lv_str ).

WRITE: / lv_str.

[ABAP] Binärdaten (xstring) mit GZIP komprimieren

DATA: lv_str TYPE string VALUE 'Teststring für die Kompression mit GZIP.'.
DATA: lv_xstr TYPE xstring.
DATA: lv_xstr_comp 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.

* compress
cl_abap_gzip=>compress_binary( EXPORTING
                                 raw_in   = lv_xstr
                               IMPORTING
                                 gzip_out = lv_xstr_comp ).
WRITE: / lv_xstr_comp.

* uncompress
cl_abap_gzip=>decompress_binary( EXPORTING
                                   gzip_in = lv_xstr_comp
                                 IMPORTING
                                   raw_out = lv_xstr ).

WRITE: / lv_xstr.

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

WRITE: / lv_str.