Skip to Content
Author's profile photo Johannes Gilbert

Hands On: Convert string encoding in ABAP


The following code snippet shows how to convert string encoding in ABAP using the functionalities of package SIXML (iXML Library: Parsing, DOM Representation, and XML Rendering).

Code Snippet(s)

      lv_content_string TYPE string, "the source string
      lv_target_encoding TYPE string DEFAULT 'UTF-8',
      lr_ixml           TYPE REF TO if_ixml,
      lr_stream_factory TYPE REF TO if_ixml_stream_factory,
      lr_encoding       TYPE REF TO if_ixml_encoding,
      lr_resstream      TYPE REF TO if_ixml_ostream,
      lv_content_xstring TYPE xstring. "the result string as binary string
    CLEAR rv_content_xstring.
*   Instanciate iXML object.
    lr_ixml = cl_ixml=>create( ).
*   Instanciate a stream factory.
    lr_stream_factory = lr_ixml->create_stream_factory( ).
*   Setup target encoding information.
    lr_encoding = lr_ixml->create_encoding(
      character_set = lv_target_encoding
      byte_order = 0 ).
*   Create the stream specifying where to push the output.
    lr_resstream = lr_stream_factory->create_ostream_xstring( lv_content_xstring ).
*   Set the output encoding.
    CALL METHOD lr_resstream->set_encoding( lr_encoding ).
*   Transform the data encoding.
      SOURCE XML lv_content_string
      RESULT XML lr_resstream.


The functionalities of class CL_IXML are used to convert the string encoding via stream processing.


Key Value
Software Component SAP_BASIS

Assigned Tags

      1 Comment
      You must be Logged on to comment or reply to a post.
      Author's profile photo Jacques Nomssi
      Jacques Nomssi

      Hello Johannes,

      for pure code page conversion check class CL_ABAP_CONV_OUT_CE, i.e.

      cl_abap_conv_out_ce=>create( encoding = 'UTF-8' )->convert(

        EXPORTING data = iv_string
        IMPORTING buffer = rv_xstring ).

      best regards,