Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Conversion utf-16 unicode -> ASCII

alexander_kutz
Participant
0 Likes
2,839

Hello,

i read a utf-16 file. The read text is diplayed like this string:

yp<#?#x#m#l# #v#e#r#s#i#o#n#=#"#1#.#0#"

How can I convert the text? I haven't found functions to do that.

1 ACCEPTED SOLUTION
Read only

Laxmana_Appana_
Active Contributor
0 Likes
2,083

Hi,

Check this :

/people/ulrich.brink/blog/2005/08/18/unicode-file-handling-in-abap

It may help.....

Regards

Appana

6 REPLIES 6
Read only

Former Member
0 Likes
2,083

When you open the dataset, use the encoding option.

open dataset dsn in input mode encoding 'utf-16'.

Regards,

ravi

Read only

0 Likes
2,083

Is that supported?

I got an error if i tried to use UTF-16.

Read only

Laxmana_Appana_
Active Contributor
0 Likes
2,084

Hi,

Check this :

/people/ulrich.brink/blog/2005/08/18/unicode-file-handling-in-abap

It may help.....

Regards

Appana

Read only

0 Likes
2,083

Thanks Appana, cl_abap_conv_in_ce seems to be the right class - but i can't get it to work. - To use it in combination with OPEN DATASET.

Unfortunately, the OPEN DATASET statement does not support UTF-16.

Read only

0 Likes
2,083

Here's a QDAC (Quick & Dirty ABAP code) to convert UTF-16 file on app server to sap default codepage file.

Tested on a non-unicode 640 system.

data: gt_line type string occurs 0 with header line,
      gv_bin type xstring,
      conv_obj type ref to cl_abap_conv_in_ce.

parameters: in_file type rlgrap-filename,
            out_file type rlgrap-filename.

start-of-selection.
  check not in_file is initial.
  open dataset in_file for input in binary mode.
  do.
    read dataset in_file into gv_bin.
    if sy-subrc ne 0.
      close dataset in_file.
      exit.
    endif.

    if sy-index eq 1.
      perform create_conv_obj.
    endif.

    try.
        call method conv_obj->convert
          exporting
            input = gv_bin
            n     = -1
          importing
            data  = gt_line.
      catch cx_sy_conversion_codepage .
      catch cx_sy_codepage_converter_init .
      catch cx_parameter_invalid_type .
    endtry.
    append gt_line.
  enddo.

  check not out_file is initial.
  open dataset out_file for output in binary mode.
  loop at gt_line.
    transfer gt_line to out_file.
  endloop.
  close dataset out_file.

*&--------------------------------------------------------------------*
*&      Form  create_conv_obj
*&--------------------------------------------------------------------*
form create_conv_obj.
  data: lv_bom(2) type x,
        lv_encoding type abap_encod,
        lv_endian type abap_endia.

  lv_bom = gv_bin.

  if lv_bom eq 'FFFE'.
    lv_encoding = '4103'.          "code page for UTF-16LE
    lv_endian = 'L'.
    shift gv_bin left by 2 places in byte mode.
  elseif lv_bom eq 'FEFF'.
    lv_encoding = '4102'.          "code page for UTF-16BE
    lv_endian = 'B'.
    shift gv_bin left by 2 places in byte mode.
  else.
    message 'Byte order mark not found at the begining of the file'
             type 'E'.
  endif.

  try.
      call method cl_abap_conv_in_ce=>create
        exporting
          encoding    = lv_encoding
          endian      = lv_endian
          replacement = '#'
        receiving
          conv        = conv_obj.
    catch cx_parameter_invalid_range .
    catch cx_sy_codepage_converter_init .
  endtry.

endform.                    "create_conv_obj

Regards

Sridhar

Read only

0 Likes
2,083

I got it. There is a bug in the sap method to create a table of an xml object. The function makes intern a wrong unicode conversion. You have to use the function SO_SOLIXTAB_TO_SOLITAB after using cl_xml_document->get_as_table to get a right conversion on a unicode system.