Skip to content

SCMS_STRING_TO_XSTRING

Encode an ABAP string to raw bytes (xstring) with explicit code page control — the standard FM for producing UTF-8 output for HTTP bodies, MIME parts, and ZIP content.

Purpose

ABAP string holds Unicode text internally. When sending data to external systems (HTTP, files, ZIP) you need raw bytes in a specific encoding. SCMS_STRING_TO_XSTRING performs that conversion.

For the reverse direction (xstring → string) use CL_ABAP_CONV_IN_CE — a matching SCMS_XSTRING_TO_STRING FM does not exist in all systems.

Signature

Parameter Direction Description
TEXT Importing Source string
MIMETYPE Importing Optional MIME type (e.g. 'text/xml') — influences default encoding
ENCODING Importing Numeric SAP code page: 4110 = UTF-8, 1100 = ISO-8859-1
BUFFER Exporting Result xstring

ENCODING requires a numeric SAP code page

ENCODING expects a numeric code page string (e.g. '4110' for UTF-8) — not a MIME name like 'UTF-8'. Passing a MIME name dumps with CX_SY_CONVERSION_NO_NUMBER.

Example — encode string to UTF-8 bytes

REPORT z_scms_encode.

START-OF-SELECTION.

  DATA: lv_text   TYPE string VALUE 'Hello, Wörld',
        lv_bytes  TYPE xstring.

  CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
    EXPORTING
      text     = lv_text
      encoding = '4110'
    IMPORTING
      buffer   = lv_bytes
    EXCEPTIONS
      failed   = 1
      OTHERS   = 2.

  IF sy-subrc <> 0.
    MESSAGE 'Encoding failed' TYPE 'E'.
    RETURN.
  ENDIF.

  WRITE: / 'Input string: ', lv_text.
  WRITE: / 'Byte length:  ', xstrlen( lv_bytes ).
  WRITE: / 'Hex bytes:    ', lv_bytes.

Example — round-trip encode then decode

Decoding uses CL_ABAP_CONV_IN_CE since SCMS_XSTRING_TO_STRING does not exist in all systems:

REPORT z_scms_roundtrip.

START-OF-SELECTION.

  DATA: lv_text   TYPE string VALUE 'Hello, Wörld',
        lv_bytes  TYPE xstring,
        lv_result TYPE string.

  " Encode to UTF-8
  CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
    EXPORTING
      text     = lv_text
      encoding = '4110'
    IMPORTING
      buffer   = lv_bytes
    EXCEPTIONS
      failed   = 1
      OTHERS   = 2.

  WRITE: / 'Original:  ', lv_text.
  WRITE: / 'Hex bytes: ', lv_bytes.

  " Decode back to string
  TRY.
      DATA(lo_conv) = cl_abap_conv_in_ce=>create(
                        encoding = '4110'
                        input    = lv_bytes ).
      lo_conv->read( IMPORTING data = lv_result ).
    CATCH cx_sy_codepage_converter_init
          cx_sy_conversion_codepage
          cx_parameter_invalid_range INTO DATA(lx).
      MESSAGE lx->get_text( ) TYPE 'E'.
      RETURN.
  ENDTRY.

  WRITE: / 'Decoded:   ', lv_result.
  WRITE: / 'Match:     ', xsdbool( lv_text = lv_result ).

Common code page numbers

Code page Character set
'4110' UTF-8 — Unicode, variable width (most common)
'4102' UTF-16 Big Endian
'4103' UTF-16 Little Endian
'1100' ISO-8859-1 — Latin-1 (Western Europe)
'1160' Windows-1252 — Windows Western European

Common pitfalls

Omitting ENCODING uses the system code page

If ENCODING is blank, the system's default code page is used (typically UTF-16 in Unicode systems). This is rarely what you want when exchanging data with external systems. Always set ENCODING explicitly.

Use CL_ABAP_CONV_OUT_CE for streaming

If you're converting large volumes of text in a loop, CL_ABAP_CONV_OUT_CE is more efficient — it keeps the conversion context open and avoids repeated FM call overhead.

See also

Comments