5

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?

Jagger
  • 10,350
  • 9
  • 51
  • 93
Felix
  • 78
  • 4
  • 15

3 Answers3

5

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( ).
Jagger
  • 10,350
  • 9
  • 51
  • 93
Felix
  • 78
  • 4
  • 15
4

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

Gert Beukema
  • 2,510
  • 1
  • 17
  • 18
  • @Jagger example for Gert's answer : `data(xstring) = cl_abap_codepage=>convert_to( source = \`paix et prospérité\` codepage = \`UTF-8\` ).` cf [ABAP doc](https://help.sap.com/http.svc/rc/abapdocu_752_index_htm/7.52/en-US/index.htm?file=abencl_abap_codepage.htm) – Sandra Rossi Feb 23 '18 at 08:03
0

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.

Marius
  • 3,372
  • 1
  • 30
  • 36