Skip to Content
Author's profile photo Timo John

Replace special characters from different european languages ( ANSI codepage unicode utf8 )

Last week I ended up with a task to convert special characters before writing them into a text file for a legacy system ( no #utf-8 support ) using :

OPEN DATASET lv_filename FOR APPENDING IN TEXT MODE ENCODING NON-UNICODE.

(Don’t forget to check the authority upfront link )

Some special letters as German “Ä ü …” are accepted but others like “šũvá” are to be replaced. Replaced at a best by a close “friend”: ũ -> u;  š -> s to avoid names Like “#av#”

CALL FUNCTION 'SCP_REPLACE_STRANGE_CHARS'

helps a lot, but I hazzled so much to find a code page that fitted to my needs.

 

 

Here is the code of a small report that converts a “dirty” string and test if it fits to my requirements.

START-OF-SELECTION.

  DATA lv_clean TYPE string.
  DATA(lv_dirty) = 'Jáošũvá Алксй Äü'.
  WRITE lv_dirty.
  
  SELECT cpcodepage FROM tcp00a INTO TABLE @DATA(lt_codepages)
    GROUP BY cpcodepage.
  

  LOOP AT lt_codepages ASSIGNING FIELD-SYMBOL(<s_codepage>).

    CALL FUNCTION 'SCP_REPLACE_STRANGE_CHARS'
      EXPORTING
        intext            = lv_dirty
        inter_cp          = <s_codepage>-cpcodepage
        replacement       = '35'        " Error Chracters are replaced as #
      IMPORTING
        outtext           = lv_clean
      EXCEPTIONS
        invalid_codepage  = 1  
        codepage_mismatch = 2  
        internal_error    = 3
        cannot_convert    = 4
        fields_not_type_c = 5
        OTHERS            = 6.

    IF sy-subrc <> 0.
      WRITE <s_codepage>-cpcodepage && '  error'.
      CONTINUE.

    ENDIF.

    " Fo me only codepages that output german special characters are relevant.
    IF contains( val = lv_clean sub = 'Äü' ).
      WRITE / |{ <s_codepage>-cpcodepage } && ` : ` && { lv_clean } | .
      CLEAR lv_clean.
    ENDIF.
  ENDLOOP.

  EXIT.

With the report I found the SAP Codepage 1157 for my requirments fast.

Be aware, that the conversion takes a while. #performance

There is a helpful wikipage on this topic as well by sandra rossi.  Showing other classes that help on this topic as well.  Thank for posting!

 

 

 


    

Assigned Tags

      2 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Michelle Crapo
      Michelle Crapo

      I don't need this now.   But I've bookmarked it for future use.

      Very nice explanation!

      Michelle

      Author's profile photo Sandra Rossi
      Sandra Rossi

      You may also use:

      OPEN DATASET filepath FOR OUTPUT IN LEGACY TEXT MODE CODE PAGE '1157' REPLACEMENT CHARACTER '#'.

      More info: ABAP Documentation of OPEN DATASET