[ABAP] Umwandlung SAP Codepage / HTTP Encoding Name

Tabellen

TCP00 (SAP Codepage Katalog)
TCP00A (Codepage-Attribute)

Variante 1 (cl_abap_codepage)

TRY.
    DATA(lv_http_codepage) = |utf-8|.
* utf-8 -> 4110
* get sap codepage by external name
    WRITE: / lv_http_codepage, '->', cl_abap_codepage=>sap_codepage( lv_http_codepage ).
  CATCH cx_root INTO DATA(e_txt).
    WRITE: / e_txt->get_text( ).
ENDTRY.

Variante 2 (cl_i18n_utils)

* Encoding Name in SAP Codepage
DATA(lv_codepage_name) = |UTF-8|.
*H  HTTP-Name
*M  MIME-Name (wird nicht mehr benutzt)
*J  Java-Name (java.io und java.lang APIs)
*W  Microsoft-Windows Codepage-Nummer
*D  Dokumentenreferenz (ID(2),OBJ(60))
*T  Kurzer, internationaler Text
*A  Passende, ASCII-basierte Codepage
*C  RTF: /cpg Wert
*F  RTF: /fcharsetN Wert
*S  Sequenz von Code Units
*G  Enthält Filter Segmente
DATA(lv_kind) = CONV cpattrkind( 'H' ).
DATA(lv_sap_codepage) = CONV cpcodepage( '' ).

* Get a codepage from an encoding name
DATA(rv) = cl_i18n_utils=>get_cp_from_name( EXPORTING im_name = lv_codepage_name
                                                      im_kind = lv_kind
                                            IMPORTING ex_cp   = lv_sap_codepage ).

CASE rv.
* successful
  WHEN 0.
    WRITE: / lv_codepage_name, '->', lv_sap_codepage.
* internal error such as old kernel
  WHEN 1.
* I/O error such as missing TCP00A
  WHEN 8.
* invalid arguments
  WHEN 16.
* not found
  WHEN 128.
ENDCASE.

Variante 3 (cl_abap_codepage)

TRY.
    DATA(lv_sap_codepage) = CONV cpcodepage( '4110' ).
* 4110 -> utf-8
* convert sap codepage to http codepage
    WRITE: / lv_sap_codepage, '->', cl_abap_codepage=>sap_to_http( lv_sap_codepage ).
  CATCH cx_root INTO DATA(e_txt).
    WRITE: / e_txt->get_text( ).
ENDTRY.

Variante 4 (cl_abap_codepage)

TRY.
    DATA(lv_sap_codepage) = CONV cpcodepage( '4110' ).
* 4110 -> UTF8
* convert sap codepage to JAVA codepage
    WRITE: / lv_sap_codepage, '->', cl_abap_codepage=>sap_to_java( lv_sap_codepage ).
  CATCH cx_root INTO DATA(e_txt).
    WRITE: / e_txt->get_text( ).
ENDTRY.

Links

[ABAP] ABAP Encoding / Codepage

Übersicht

* Typgruppe ABAP
* Typ: ABAP_ENCOD
* Tabelle: TCP00A

Codepage  Encoding
---------------------
1100      iso-8859-1
1101      ascii
1105      us-ascii (7 bits)
1160      windows-1252
1401      iso-8859-2
4102      utf-16be
4103      utf-16le
4110      utf-8

Links

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

Variante 1 (cl_abap_conv_in_ce)

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.

Variante 2 (/ui2/cl_abap2json)

DATA: lv_xstr TYPE xstring VALUE '54657374737472696E672066C3BC7220646965204B6F6D7072657373696F6E206D697420475A49502E'.

* xstring -> string (UTF-8)
DATA(lv_str) = /ui2/cl_abap2json=>conv_xstring_to_string( lv_xstr ).

WRITE: / lv_str.

Variante 3 (cl_proxy_service)

DATA: lv_xstr TYPE xstring VALUE '54657374737472696E672066C3BC7220646965204B6F6D7072657373696F6E206D697420475A49502E'.

* xstring (binary) -> string (4110)
DATA(lv_str_res) = cl_proxy_service=>xstring2cstring( lv_xstr ).

WRITE: / lv_str_res.

Variante 4 (ICT_DISPATCH)

DATA: lv_xstr TYPE xstring VALUE '54657374737472696E672066C3BC7220646965204B6F6D7072657373696F6E206D697420475A49502E'.
DATA: lv_string TYPE string.

* xstring (binary) -> string
CALL 'ICT_DISPATCH' ID 'did'    FIELD 'append_xstring_to_string'
                    ID 'source' FIELD lv_xstr
                    ID 'dest'   FIELD lv_string.

WRITE: / lv_string.

[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.