CL_ABAP_CONV_OUT_CE / CL_ABAP_CONV_IN_CE¶
Convert ABAP strings to/from raw byte sequences in a specific character encoding.
Purpose¶
ABAP strings are internally Unicode (UCS-2). When you need to produce or consume bytes in a specific encoding — UTF-8 for HTTP, ISO-8859-1 for legacy files — these two classes handle the conversion:
CL_ABAP_CONV_OUT_CE— string → xstring (encoding outbound)CL_ABAP_CONV_IN_CE— xstring → string (encoding inbound)
These are instance methods, not static
CL_ABAP_CONV_OUT_CE and CL_ABAP_CONV_IN_CE must be instantiated via their CREATE factory method. WRITE and READ are called on the instance, not on the class directly.
Key methods¶
CL_ABAP_CONV_OUT_CE¶
| Method | Description |
|---|---|
CREATE( encoding = '...' ) |
Factory — returns converter instance |
WRITE( data = lv_string ) |
Feed a string chunk |
GET_BUFFER( ) |
Returns accumulated xstring result |
CL_ABAP_CONV_IN_CE¶
| Method | Description |
|---|---|
CREATE( encoding = '...' input = lx_bytes ) |
Factory with source xstring |
READ( n = -1 ) IMPORTING data, len |
Read n units from input xstring into string; -1 reads all |
Examples¶
String → UTF-8 xstring¶
REPORT z_conv_out_demo.
START-OF-SELECTION.
DATA: lv_string TYPE string VALUE 'Hello SAP World',
lx_utf8 TYPE xstring.
TRY.
DATA(lo_conv) = cl_abap_conv_out_ce=>create( encoding = 'UTF-8' ).
lo_conv->write( data = lv_string ).
lx_utf8 = lo_conv->get_buffer( ).
CATCH cx_sy_conversion_codepage INTO DATA(lx).
MESSAGE lx->get_text( ) TYPE 'E'.
ENDTRY.
WRITE: / 'xstring length:', xstrlen( lx_utf8 ).
xstring → ABAP string (UTF-8 decode)¶
REPORT z_conv_in_demo.
START-OF-SELECTION.
" First encode a string to xstring
DATA(lo_out) = cl_abap_conv_out_ce=>create( encoding = 'UTF-8' ).
lo_out->write( data = 'Hello SAP World' ).
DATA(lx_bytes) = lo_out->get_buffer( ).
" Now decode it back
DATA lv_result TYPE string.
TRY.
DATA(lo_in) = cl_abap_conv_in_ce=>create(
encoding = 'UTF-8'
input = lx_bytes ).
lo_in->read(
IMPORTING data = lv_result ).
CATCH cx_sy_conversion_codepage INTO DATA(lx).
MESSAGE lx->get_text( ) TYPE 'E'.
ENDTRY.
WRITE: / lv_result. " Hello SAP World
ISO-8859-1 xstring → ABAP string (legacy file content)¶
REPORT z_conv_iso_demo.
START-OF-SELECTION.
" Simulate reading a legacy ISO-8859-1 encoded file
" (in practice lx_raw comes from GUI_UPLOAD with filetype BIN)
" SAP code page 1100 = ISO-8859-1 / Latin-1
DATA lo_out TYPE REF TO cl_abap_conv_out_ce.
DATA lx_raw TYPE xstring.
lo_out = cl_abap_conv_out_ce=>create( encoding = '1100' ).
lo_out->write( data = 'Legacy text with special chars' ).
lx_raw = lo_out->get_buffer( ).
" Decode it
DATA lv_text TYPE string.
TRY.
DATA(lo_in) = cl_abap_conv_in_ce=>create(
encoding = '1100' " SAP code page 1100 = ISO-8859-1
input = lx_raw ).
lo_in->read(
IMPORTING data = lv_text ).
CATCH cx_sy_conversion_codepage
cx_sy_codepage_converter_init INTO DATA(lx).
MESSAGE lx->get_text( ) TYPE 'E'.
ENDTRY.
WRITE: / lv_text.
Use SAP code page numbers for non-UTF encodings
'UTF-8' works as a MIME name. For legacy encodings use the SAP numeric code page ID — MIME names like 'iso-8859-1' are not reliably recognized and will raise CX_SY_CODEPAGE_CONVERTER_INIT. Common SAP code page numbers:
| Code page | Encoding |
|---|---|
'4110' |
UTF-8 |
'4103' |
UTF-16BE |
'4102' |
UTF-16LE |
'1100' |
ISO-8859-1 / Latin-1 |
'1160' |
Windows-1252 |
Check all available code pages in transaction SCPG.
See also¶
CL_ABAP_CHAR_UTILITIES— control character constantsCL_ABAP_ZIP— create ZIP archives (uses xstring)