Skip to Content
1
Sep 04, 2019 at 09:22 AM

ABAP character encoding conversion

269 Views Last edit Sep 04, 2019 at 09:30 AM 3 rev

Hello,

I'm trying to export a data file in encoding ISO-8859-15. This code type allows characters like €, Š, š, Ž, ž, Œ, œ or Ÿ.

I've looked into the ABAP tools available for this purpose, especially thru this great blog from Sandra Rossi.

Here you can find my testing code:

*&---------------------------------------------------------------------*
*& Report ZBC_FILE_ENCODING
*&---------------------------------------------------------------------*
*& Encoding test report
*&---------------------------------------------------------------------*

REPORT zbc_file_encoding.

DATA: gv_file              TYPE text255,
      gt_file              LIKE STANDARD TABLE OF gv_file,
      gv_filename          TYPE string,
      gv_path              TYPE string,
      gv_fullpath          TYPE string,
      gv_bin_filesize      TYPE i,
      gt_bin_data          TYPE solix_tab,
      gv_xfile             TYPE xstring,
      gv_sfile             TYPE string,
      gv_sap_codepage      TYPE cpcodepage,
      gv_default_file_name TYPE string,
      gv_external_name     TYPE tcp00a-cpattr,
      go_abap_conv_obj     TYPE REF TO cl_abap_conv_obj,
      gv_incode            TYPE cpcodepage,
      gv_outcode           TYPE cpcodepage.

gv_file = 'A;B;C;é;€;Š;š;Ž;ž;Œ;œ;Ÿ'.
APPEND gv_file TO gt_file.
gv_file = 'D;E;F;é;€;Š;š;Ž;ž;Œ;œ;Ÿ'.
APPEND gv_file TO gt_file.

LOOP AT gt_file INTO gv_file.
  CONCATENATE gv_sfile gv_file cl_abap_char_utilities=>cr_lf INTO gv_sfile.
ENDLOOP.

gv_external_name = 'ISO-8859-15'.

CALL FUNCTION 'SCP_CODEPAGE_BY_EXTERNAL_NAME'
  EXPORTING
    external_name = gv_external_name
  IMPORTING
    sap_codepage  = gv_sap_codepage
  EXCEPTIONS
    OTHERS        = 1.

IF sy-subrc NE 0.
  MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

CALL FUNCTION 'SCP_GET_CODEPAGE_NUMBER'
  EXPORTING
    database_also = ' '
  IMPORTING
    appl_codepage = gv_incode
  EXCEPTIONS
    OTHERS        = 1.

IF sy-subrc NE 0.
  MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

gv_outcode = gv_sap_codepage.

CREATE OBJECT go_abap_conv_obj
  EXPORTING
    incode   = gv_incode
    outcode  = gv_outcode
    miss     = 'S'
    ctrlcode = '.'
  EXCEPTIONS
    OTHERS   = 1.

IF sy-subrc NE 0.
  MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

CALL METHOD go_abap_conv_obj->convert
  EXPORTING
    inbuff    = gv_sfile
    outbufflg = gv_bin_filesize
  IMPORTING
    outbuff   = gv_xfile
  EXCEPTIONS
    OTHERS    = 1.

IF sy-subrc NE 0.
  MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
  EXPORTING
    buffer        = gv_xfile
  IMPORTING
    output_length = gv_bin_filesize
  TABLES
    binary_tab    = gt_bin_data.

CONCATENATE 'test_encoding_' gv_external_name '.csv' INTO gv_default_file_name.

CALL METHOD cl_gui_frontend_services=>file_save_dialog
  EXPORTING
    default_file_name = gv_default_file_name
  CHANGING
    filename          = gv_filename
    path              = gv_path
    fullpath          = gv_fullpath
  EXCEPTIONS
    OTHERS            = 1.

IF sy-subrc NE 0.
  MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

CALL METHOD cl_gui_frontend_services=>gui_download
  EXPORTING
    bin_filesize         = gv_bin_filesize
    filename             = gv_fullpath
    filetype             = 'BIN'
    show_transfer_status = ' '
  CHANGING
    data_tab             = gt_bin_data
  EXCEPTIONS
    OTHERS               = 1.

IF sy-subrc NE 0.
  MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

Unfortunately, at the end, I don't get what I'm expecting:

Notepad++ says that the format is Windows-1252, the special characters are messed up and even the carriage return and line feed are not recognized.

Any idea of what I'm doing wrong and how to achieve my goal?

Thanks in advance for your help.

Best regards,

Marco Silva

Attachments

result.png (15.6 kB)