Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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().

    CALL FUNCTION 'SCP_REPLACE_STRANGE_CHARS'
      EXPORTING
        intext            = lv_dirty
        inter_cp          = -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 -cpcodepage && '  error'.
      CONTINUE.

    ENDIF.

    " Fo me only codepages that output german special characters are relevant.
    IF contains( val = lv_clean sub = 'Äü' ).
      WRITE / |{ -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!

                                


This post first appeared on Web Service Testing A Blackjack GUI And API, please read the originial post: here

Share the post

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

×

Subscribe to Web Service Testing A Blackjack Gui And Api

Get updates delivered right to your inbox!

Thank you for your subscription

×