5
votes

I want to convert a string into a xstring. I know that there is a function module named "SCMS_STRING_TO_XSTRING"

But since it is not a good habit to use function modules anymore, a class based solution would be my prefered way to go.

I know that there is a class

cl_abap_conv_in_ce

but I can only validate, that this class can convert xstrings into string. I wand to have the reverse case. Does anyone have experience on how to do that class based?

3

3 Answers

5
votes

Meanwhile, I found the solution on my own. For people who might be interested:

    DATA(lo_conv) = cl_abap_conv_out_ce=>create( ).
    lo_conv->write( data = lv_content ).
    DATA(lv_xstring) = lo_conv->get_buffer( ).
3
votes

The help text for XSTRING provides a nice functional method for this: cl_abap_codepage=>convert_to( )

0
votes

Firstly, you need to decide how you want it encoded. UTF-8? UTF-16? Just plain HEX?

For UTF-8 You can do the following using system calls (instead of function calls):

First do a global once-off initialization:

  STATICS: g_conv_utf8 TYPE xstring. " used for conversion

  DATA: l_flags TYPE c LENGTH 1.
  system-call convert id 20
      srcenc 'SET LOCALE LANGUAGE'
      dstenc 'UTF-8'
      replacement '#'
      type l_flags
      cinfo g_conv_utf8.

And then do subsequent calls: l_string -> l_xstring (+ l_len)

  SYSTEM-CALL CONVERT ID 24
    DATA l_string
    ENDIAN ' '
    IGNORE_CERR 'X'
    N -1
    BUFFER l_xstring
    LEN l_length
    CINFO g_conv_utf_8.

This is the essence of what cl_abap_codepage=>convert_to( ) does internally.