Application Development 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: 

GUI_DOWNLOAD give 2 bytes for each chinese character - I need fixed length

Former Member
0 Kudos

Due to the Unicode system takes each Chinese character as a byte, the field(10) can take 10 Chinese Characters, not like before only 5 characters allowed.

The problem is when the data is downloaded to a txt file. Some records contains only Western Europe charactes and will fill 10 bytes in the output file and other records contains only Chinese characters and will fill up 20 bytes in the output file.

The data is exported for a system that can upload chinese characters, but only allow fixed record length file (always 10 bytes).

In other words: the records with Western Europe characters should contain 10 characters = 10 bytes but the records with Chinese characters should only contain 5 characters = 10 bytes.

Anyone who can solve this, thanks in advance.

My code:

  CALL METHOD CL_GUI_FRONTEND_SERVICES=>GUI_DOWNLOAD
    EXPORTING
      FILENAME                = p_file
      CONFIRM_OVERWRITE       = 'X'
      codepage                = '8400' "Chinese
    CHANGING
      DATA_TAB                = it_output
    EXCEPTIONS
...

4 REPLIES 4

Former Member
0 Kudos

Hi Thomas,

Yes in unicode sys. 1 char <> 1 byte. So 1 chinese char = 2 bytes. If u download 10 chinese chars then it is equivalent to 20 bytes. As u mentioned that u need to upload these 10 chinese chars to an external system which is fixed in length and can take only 10 bytes. Now if in that system 1 char = 1 byte then it can take 10 chinese char. Then what is the prob? May be I am not understand ur prob. properly. Sorry for that.

Regards,

Joy.

0 Kudos

Hi Joy

Thank for your reply

Let me quote myself:

The data is exported for a system that can upload chinese characters, but only allow fixed record length file (always 10 bytes).

By this i mean that the system can only upload a file where the 10 char field fill 10 bytes, but as you already understand SAP will export 20 bytes.

To get a file with only 10 bytes it's ok to throw away the last 5 chars of a chinese text, but it must keep the 10 characters of english texts.

I hope this makes my problem more clear.

Best regards

Thomas Madsen Nielsen

0 Kudos

Hi Thomas,

Now I understand.

Pl. check the class CL_ABAP_CONV_X2X_CE.

May it can help u.

Here is it's documentation.

CL CL_ABAP_CONV_X2X_CE

____________________________________________________

Short Text

Code Page and Endian Conversion Between External Formats

Functionality

Instances of the class CL_ABAP_CONV_X2X_CE allow you to convert text data between different character sets and numeric data between different number formats (byte order).

Using the class corresponds to transforming data from a binary input stream to a binary output stream: The data objects are read from the input buffer sequentially and written to an output buffer.

The class methods are normally used as follows:

CL_ABAP_CONV_X2X_CE=>CREATE

This creates a conversion instance. Among other options, you can specify the following as parameters: The input buffer (that is the binary string that contains the data to be read), the character format at used in the input buffer, the byte order used in the input buffer, the character format to be used in the output buffer, or the byte order to be used in the output buffer.

CONVERT_C, CONVERT_...

This converts the data. Calling this method several times consecutively allows you to read data from the input buffer sequentially and add it to the end of the output buffer.

GET_OUT_BUFFER

This gets the output buffer, making it available for further processing (such as writing to a file, sending using RFC).

RESET

This allows you to reset individual attributes of the conversion instance. (This method is especially designed for restarting on a new input buffer and deleting the output buffer while retaining the remaining attributes.)

Relationships

CL_ABAP_CONV_IN_CE

Converting Binary Data into ABAP Data Objects

CL_ABAP_CONV_OUT_CE

Converting ABAP Data Objects to a Binary Format.

CL_ABAP_CHAR_UTILITIES

Various Attributes and Methods for Character Sets and Byte Order

Example

In the following example, UTF-8 texts are converted to the codepage 1100 (Latin-1) and numbers are converted from little-endian to big-endian format:

DATA:

in_buffer TYPE XSTRING,

out_buffer TYPE XSTRING,

conv TYPE REF TO cl_abap_conv_x2x_ce.

in_buffer = '414220C3B602010000'.

conv = cl_abap_conv_x2x_ce=>create(

in_encoding = 'UTF-8'

in_endian = 'L'

out_encoding = '1100'

out_endian = 'B'

input = in_buffer

).

CALL METHOD conv->convert_c( n = 5 ).

CALL METHOD conv->convert_i( ).

out_buffer = conv->get_out_buffer( ).

The content of the in_buffer variable is made up of 5 bytes (hexadecimal "414220C3B6"), which represent 4 UTF-8 characters (A, B, SPACE, o-Umlaut), and 4 bytes (hexadecimal "02010000"), which represent the value 258 in little-endian format. This data is converted and the out_buffer variable now contains the hexadecimal string "414220F600000102". It is made up of:

4 bytes (hexadecimal "414220F6"), which represent the above 4 characters in codepage 1100 (ISO-8859-1). Note that the length specification n = 5 refers to the number of elementary characters to be converted. For multi-byte codepages like UTF-8 this means that the multi-byte character "C3B6" is counted in length 2.

4 Bytes (hexadecimal "00000102"), which represent the value 258 in big-endian format.

If you have the desired byte order in the old format as expected by TRANSLATE ... NUMBER FORMAT (as an N(4) value), you can proceed as follows:

TYPE-POOLS: ABAP.

CLASS cl_abap_char_utilities DEFINITION LOAD.

DATA:

in_number_format(4) TYPE N VALUE '0101',

out_number_format(4) TYPE N VALUE '0000'.

DATA:

in_endian TYPE ABAP_ENDIAN,

out_endian TYPE ABAP_ENDIAN,

conv TYPE REF TO cl_abap_conv_x2x_ce..

in_endian = cl_abap_char_utilities=>number_format_to_endian(

in_number_format

).

out_endian = cl_abap_char_utilities=>number_format_to_endian(

out_number_format

).

conv = cl_abap_conv_x2x_ce=>create(

in_encoding = 'UTF-8'

in_endian = in_endian

out_encoding = '1100'

out_endian = out_endian

input = in_buffer

).

Notes

For details refer to the documentation for the individual methods.

Regards,

Joy.

0 Kudos

Hi again

I made this solution:


      begin of rec_output2,
        hkont(10),
        dmbtr(18),                                          "3 decimals
        shkzg,           "S=D others=C
        sgtxt(25),
        bewar(1),       "V/I/D
        waers(5),
      end of rec_output2,
      begin of rec_output3,
        hkont(10) type x,
        dmbtr(18) type x,                                   "3 decimals
        shkzg type x,           "S=D others=C
        sgtxt(25) type x,
        bewar(1) type x,       "V/I/D
        waers(5) type x,
      end of rec_output3,
      begin of rec_output,
        record(59) type x,
      end of rec_output,
      it_output like standard TABLE OF rec_output.
field-SYMBOLS: <fs_output> like line of it_output.

...

    PERFORM translate_codepage using rec_output2 rec_output3.
    append INITIAL LINE TO it_output ASSIGNING <fs_output>.
    <fs_output> = rec_output3.

...

  CALL METHOD CL_GUI_FRONTEND_SERVICES=>GUI_DOWNLOAD
    EXPORTING
      FILENAME                = p_file
      CONFIRM_OVERWRITE       = 'X'
    CHANGING
      DATA_TAB                = it_output
    EXCEPTIONS
      FILE_WRITE_ERROR        = 1
     others                  = 24.
  IF SY-SUBRC <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
               WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.


*&---------------------------------------------------------------------*
*&      Form  translate_codepage
*&---------------------------------------------------------------------*
* The output file must be fixed lengt file with fixed field lenght, so
* a special code page translation is required
* Demo program RSCP0032 is used as inspiration for this rutine
*----------------------------------------------------------------------*
FORM translate_codepage using p_recin p_recout.
  FIELD-SYMBOLS: <FS_inFIELD> TYPE ANY,
                 <fs_outfield> type any.
  DATA: conv_obj TYPE REF TO cl_abap_conv_obj,
        L_FIELDLENGTH TYPE I,
        L_OUT_XSTRING TYPE XSTRING.


  w_in_codepage = '4103'.    "system code page
  w_out_codepage = '8400'.  "Chinese code page

  DO.
    ASSIGN COMPONENT SY-INDEX OF STRUCTURE p_recin TO <FS_inFIELD>.
    IF SY-SUBRC NE 0.
      EXIT.
    ENDIF.
    ASSIGN COMPONENT SY-INDEX OF STRUCTURE p_recout TO <FS_outfield>.
    IF SY-SUBRC NE 0.
      EXIT.
    ENDIF.

    DESCRIBE FIELD <FS_inFIELD> LENGTH L_FIELDLENGTH IN CHARACTER MODE.

    CREATE OBJECT conv_obj
      EXPORTING
        incode           = W_in_codepage
        outcode          = W_out_cODEpAGE
        ctrlcode         = '.'
        sapownch         = '.'
      EXCEPTIONS
        invalid_codepage = 1
        internal_error   = 2
        others           = 3.

    CALL METHOD conv_obj->convert
      EXPORTING
        inbuff         = <FS_inFIELD>
        outbufflg      = L_FIELDLENGTH
      IMPORTING
        outbuff        = L_out_xstring
      EXCEPTIONS
        internal_error = 1
        OTHERS         = 2.
    MOVE L_OUT_XSTRING TO <FS_outFIELD>.
    FREE conv_obj.
  ENDDO.
ENDFORM. " translate_codepage