""--------------------------------------Program if we have Physical File-------------------------------
REPORT ysap_shiftjis_to_utf8_csv.
PARAMETERS:
"" Pick the file from system
p_infile TYPE ibipparms-path,
""Assign a path and file name for output file path
p_out TYPE string.
DATA:
lt_raw_data TYPE STANDARD TABLE OF x255,
lv_xstring TYPE xstring,
lv_string TYPE string,
lv_utf8_xstr TYPE xstring,
lt_output_raw TYPE STANDARD TABLE OF x255,
lv_filelen TYPE i,
lv_file TYPE string,
lv_encode TYPE string VALUE 'UTF-8'.
CLASS cl_abap_conv_in_ce DEFINITION LOAD.
CLASS cl_abap_conv_out_ce DEFINITION LOAD.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_infile.
CALL FUNCTION 'F4_FILENAME'
EXPORTING
program_name = syst-cprog
dynpro_number = syst-dynnr
IMPORTING
file_name = p_infile.
START-OF-SELECTION.
"" Step 1: Upload Shift-JIS file as binary
lv_file = p_infile.
CALL METHOD cl_gui_frontend_services=>gui_upload
EXPORTING
filename = lv_file
filetype = 'BIN'
IMPORTING
filelength = lv_filelen
CHANGING
data_tab = lt_raw_data.
IF sy-subrc <> 0.
MESSAGE 'File upload failed' TYPE 'E'.
ENDIF.
" Step 2: Convert RAW table to XSTRING
CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
EXPORTING
input_length = lv_filelen
IMPORTING
buffer = lv_xstring
TABLES
binary_tab = lt_raw_data
EXCEPTIONS
failed = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE 'Conversion of RAW to XSTRING failed' TYPE 'E'.
ENDIF.
" Step 3: Decode Shift-JIS XSTRING to ABAP string
DATA(lo_in) = cl_abap_conv_in_ce=>create( encoding = '8000' ).
lo_in->convert(
EXPORTING
input = lv_xstring
IMPORTING
data = lv_string ).
""---Optional Thing as I need to add UTF-8 at first line in CSV File
CONCATENATE lv_encode
lv_string
INTO lv_string
SEPARATED BY cl_abap_char_utilities=>newline.
" Step 4: Encode ABAP string to UTF-8 XSTRING
DATA(lo_out) = cl_abap_conv_out_ce=>create( encoding = '4110' ).
lo_out->convert(
EXPORTING
data = lv_string
IMPORTING
buffer = lv_utf8_xstr ).
" Step 5: Convert UTF-8 XSTRING back to RAW table
CLEAR lt_output_raw.
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = lv_utf8_xstr
TABLES
binary_tab = lt_output_raw
EXCEPTIONS
failed = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE 'XSTRING to binary conversion failed' TYPE 'E'.
ENDIF.
" Step 6: Download as UTF-8 file
CALL METHOD cl_gui_frontend_services=>gui_download
EXPORTING
filename = p_out
filetype = 'BIN'
CHANGING
data_tab = lt_output_raw
EXCEPTIONS
file_write_error = 1
no_batch = 2
gui_refuse_filetransfer = 3
invalid_type = 4
unknown_error = 5
OTHERS = 6.
IF sy-subrc = 0.
MESSAGE 'File converted successfully to UTF-8' TYPE 'S'.
ELSE.
MESSAGE 'Download failed' TYPE 'E'.
ENDIF.
"----------------------------If the data is already at AL11 Server New Report-----------------------
REPORT zsjis_to_utf8_al11.
PARAMETERS:
""->> Where Data is already stored can be hardcode too but make sure you know the name correctly
p_inset TYPE string OBLIGATORY DEFAULT 'CONVO_SHIFTJIS.CSV', " AL11 Shift-JIS DATASET
""->> Where you are going to push the data you don't know the name can assign new name everytime based parameter.
p_outset TYPE string OBLIGATORY DEFAULT 'CONVO_UTF8_CHANGE_NEW'. " AL11 UTF-8 output\\\\
DATA:
lt_raw_data TYPE STANDARD TABLE OF x255,
lv_xstring TYPE xstring,
lv_string TYPE string,
lv_utf8_xstr TYPE xstring,
lt_utf8_raw TYPE STANDARD TABLE OF x255,
lv_line TYPE x255,
lv_filelen TYPE i.
CONSTANTS : lc_encode TYPE string VALUE 'UTF-8'.
CLASS cl_abap_conv_in_ce DEFINITION LOAD.
CLASS cl_abap_conv_out_ce DEFINITION LOAD.
*---------------------------------------------------------------------*
* 1. Read Shift-JIS file from AL11 in binary mode
*---------------------------------------------------------------------*
OPEN DATASET p_inset FOR INPUT IN BINARY MODE.
IF sy-subrc <> 0.
MESSAGE |Cannot open input file: { p_inset }| TYPE 'E'.
ENDIF.
CLEAR lt_raw_data.
DO.
READ DATASET p_inset INTO lv_line.
IF sy-subrc <> 0.
EXIT.
ENDIF.
APPEND lv_line TO lt_raw_data.
ENDDO.
CLOSE DATASET p_inset.
lv_filelen = lines( lt_raw_data ) * 255.
*---------------------------------------------------------------------*
* 2. Convert Binary → XSTRING
*---------------------------------------------------------------------*
CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
EXPORTING
input_length = lv_filelen
IMPORTING
buffer = lv_xstring
TABLES
binary_tab = lt_raw_data
EXCEPTIONS
OTHERS = 1.
IF sy-subrc <> 0.
MESSAGE 'Conversion to XSTRING failed' TYPE 'E'.
ENDIF.
*---------------------------------------------------------------------*
* 3. Decode Shift-JIS (8000) to ABAP string
*---------------------------------------------------------------------*
DATA(lo_in) = cl_abap_conv_in_ce=>create( encoding = '8000' ).
lo_in->convert(
EXPORTING
input = lv_xstring
IMPORTING
data = lv_string ).
*---------------------------------------------------------------------*
* 4. Adding UTF-8 on Top
*---------------------------------------------------------------------*
CONCATENATE lc_encode
lv_string
INTO lv_string
SEPARATED BY cl_abap_char_utilities=>newline.
*---------------------------------------------------------------------*
* 4. Encode to UTF-8 XSTRING
*---------------------------------------------------------------------*
DATA(lo_out) = cl_abap_conv_out_ce=>create( encoding = '4110' ). " UTF-8
lo_out->convert(
EXPORTING
data = lv_string
IMPORTING
buffer = lv_utf8_xstr ).
*---------------------------------------------------------------------*
* 5. Convert UTF-8 XSTRING → Binary
*---------------------------------------------------------------------*
CLEAR lt_utf8_raw.
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = lv_utf8_xstr
TABLES
binary_tab = lt_utf8_raw
EXCEPTIONS
OTHERS = 1.
IF sy-subrc <> 0.
MESSAGE 'UTF-8 conversion failed' TYPE 'E'.
ENDIF.
*---------------------------------------------------------------------*
* 6. Write UTF-8 binary to AL11
*---------------------------------------------------------------------*
OPEN DATASET p_outset FOR OUTPUT IN BINARY MODE.
IF sy-subrc <> 0.
MESSAGE |Cannot open output file: { p_outset }| TYPE 'E'.
ENDIF.
LOOP AT lt_utf8_raw INTO lv_line.
TRANSFER lv_line TO p_outset.
ENDLOOP.
CLOSE DATASET p_outset.
MESSAGE |Shift-JIS to UTF-8 conversion completed: { p_outset }| TYPE 'S'.
"#SAP
"SAP Ariba Procurement, cloud edition
For any queries :
Reachout to me - prabhatm580@gmail.com
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 153 | |
| 45 | |
| 40 | |
| 21 | |
| 14 | |
| 13 | |
| 12 | |
| 12 | |
| 9 | |
| 9 |