您的位置:首页 > 其它

ABAP--关于Unicode的常见错误和解决方法

2011-02-09 09:47 2176 查看
原文地址: http://blog.csdn.net/CompassButton/archive/2010/10/12/5935088.aspx
最近也碰到一些Unicode的问题, 转贴备查.

Unicode

Unicode is the international character encoding standard that allows
the systems to handle text data from multiple languages simultaneously
and consistently. In fact, more than 5,000 SAP customer installations
are already purely Unicode-based, and the relative share of pure Unicode
installations is growing rapidly. Many companies are adopting Web
services to gain benefits such as greater openness that extends
processes to customers and business partners.

Service-oriented
architectures (SOAs), including SAP's Enterprise Services
Architecture,rely on a set of standards that enable global
interoperability across systems, programming languages, and application
services.

One of these required standards is Unicode.Much of the
enterprise software out there is already completely
Unicode-ready.Everything in the Java space and everything based on XML
is Unicode by definition.New system installations will have to be
Unicode only in future releases and new SAP products will only be
offered in Unicode.In fact, SAP NetWeaver Portal and SAP NetWeaver
Exchange Infrastructure (XI) are already Unicode-only.Each character has
a unique number („Unicode code point") Notation U+nnnn (where nnnn are
hexadecimal digits) See http://www.unicode.org
for complete code charts

Note:

Unicode flag ( "Unicode checks active") is used as a Program Attribute to control the Unicode enabling procedure



A
program without Unicode flag is NOT executable on a Unicode
system. Transaction UCCHECK can be used as tool to analyze customer
coding for unicode errors.



Common Unicode Errors and Their Corrections:

1. Error regarding OPEN DATASET:


Before Unicode:

OPEN DATASET P_UNIX FOR OUTPUT IN TEXT MODE.

Resolution:

OPEN DATASET P_UNIX FOR OUTPUT IN TEXT MODE ENCODING NON-UNICODE.

OR

OPEN DATASET P_UNIX FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.

OR

OPEN DATASET P_UNIX FOR OUTPUT IN LEGACY TEXT MODE.

The
choice of resolution depends on the kind of data the file contains. The
addition ENCODING defines how the characters are represented in the
text file. When writing to a text file, the content of a data object is
converted to the representation entered after ENCODING, and transferred
to the file. The same rule is followed for reading files using the OPEN
DATASET command.

Before Unicode:

OPEN DATASET P_UNIX FOR OUTPUT IN BINARY MODE.

Resolution:

OPEN DATASET P_UNIX FOR OUTPUT IN LEGACY BINARY MODE.

Note:
Some time it is possible that mode has not been specified in old
system, then by default system will take it as BINARY mode, We should
add LEGACY BINARY mode for such syntax.

2. Error regarding WS_UPLOAD/WS_DOWNLOAD.


Before Unicode:

Parameters: filename (128) lower case.

CALL FUNCTION 'WS_UPLOAD'

EXPORTING

FILENAME = filename

FILETYPE = 'DAT'

TABLES

DATA_TAB = I_INREC

EXCEPTIONS

FILE_OPEN_ERROR = 1

FILE_READ_ERROR = 2

CONVERSION_ERROR = 3

INVALID_TABLE_WIDTH = 4

INVALID_TYPE = 5

NO_BATCH = 6

UNKNOWN_ERROR = 7.

Resolution:

Data:w_filename TYPE STRING.

Field-symbols: <fs_filename> type any.

Assign filename to <fs_filename>.

Move <fs_filename> to w_filename.

CALL FUNCTION 'GUI_UPLOAD'

EXPORTING

FILENAME = w_filename

FILETYPE = 'DAT'

TABLES

DATA_TAB = I_INREC

EXCEPTIONS

FILE_OPEN_ERROR = 1

FILE_READ_ERROR = 2

NO_BATCH = 3

GUI_REFUSE_FILETRANSFER = 4

INVALID_TYPE = 5

NO_AUTHORITY = 6

UNKNOWN_ERROR = 7

BAD_DATA_FORMAT = 8

HEADER_NOT_ALLOWED = 9

SEPARATOR_NOT_ALLOWED = 10

HEADER_TOO_LONG = 11

UNKNOWN_DP_ERROR = 12

ACCESS_DENIED = 13

DP_OUT_OF_MEMORY = 14

DISK_FULL = 15

DP_TIMEOUT = 16

OTHERS = 17.

The
Function Modules WS_UPLOAD / WS_DOWNLOAD is obsolete and hence need to
be replaced by their new counterparts' viz. GUI_UPLOAD / GUI_DOWNLOAD.

3.Error regarding UPLOAD / DOWNLOAD:



Before Unicode:

DATA: V_FILENAME TYPE STRING value 'c: /test.txt'.

CALL FUNCTION 'DOWNLOAD'

EXPORTING

FILENAME = 'c: /test.txt'

FILETYPE = 'ASC'

TABLES

DATA_TAB = CO_TAB

EXCEPTIONS

INVALID_FILESIZE = 01

INVALID_TABLE_WIDTH = 02

INVALID_TYPE = 03

NO_BATCH = 04

UNKNOWN_ERROR = 05.

Resolution:

data: filename TYPE string.

data: ftab type filetable.

data: wa_ftab TYPE LINE OF filetable.

data: r_code type sy-subrc.

move 'c: /test.txt' to filename .

If filename CA '.'.

Else.

CONCATENATE filename '*' into filename.

endif.

CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG

EXPORTING

DEFAULT_FILENAME = filename

CHANGING

FILE_TABLE = ftab

RC = r_code.

IF NOT ftab[] IS INITIAL.

read table ftab into wa_ftab index 1.

move wa_ftab-filename to filename.

CALL FUNCTION 'GUI_DOWNLOAD'

EXPORTING

FILENAME = filename

FILETYPE = 'ASC'

TABLES

DATA_TAB = CO_TAB

EXCEPTIONS

FILE_WRITE_ERROR = 1

NO_BATCH = 2

GUI_REFUSE_FILETRANSFER = 3

INVALID_TYPE = 4

NO_AUTHORITY = 5

UNKNOWN_ERROR = 6

HEADER_NOT_ALLOWED = 7

SEPARATOR_NOT_ALLOWED = 8

FILESIZE_NOT_ALLOWED = 9

HEADER_TOO_LONG = 10

DP_ERROR_CREATE = 11

DP_ERROR_SEND = 12

DP_ERROR_WRITE = 13

UNKNOWN_DP_ERROR = 14

ACCESS_DENIED = 15

DP_OUT_OF_MEMORY = 16

DISK_FULL = 17

DP_TIMEOUT = 18

FILE_NOT_FOUND = 19

DATAPROVIDER_EXCEPTION = 20

CONTROL_FLUSH_ERROR = 21

OTHERS = 22.

Endif.

The
Function Modules UPLOAD / DOWNLOAD is obsolete and hence need to be
replaced by their new replacements GUI_UPLOAD / GUI_DOWNLOAD. Along
with their counterparts, we need to add the Function Module of
F4_Filename to read the filename at runtime.

4.Error regarding WS_EXECUTE


Before Unicode:

CALL FUNCTION 'WS_EXECUTE'

EXPORTING

DOCUMENT = 'SAP SERVICE MARKETPLACE'

EXCEPTIONS

FRONTEND_ERROR = 1

NO_BATCH = 2

PROG_NOT_FOUND = 3

ILLEGAL_OPTION = 4

OTHERS = 5.

Resolution:

CALL METHOD CL_GUI_FRONTEND_SERVICES=>EXECUTE

EXPORTING

DOCUMENT = 'https: //websmp206.sap-ag.de/'

DEFAULT_DIRECTORY = 'SAP SERVICE MARKETPLACE'

EXCEPTIONS

CNTL_ERROR = 1

ERROR_NO_GUI = 2

BAD_PARAMETER = 3

FILE_NOT_FOUND = 4

PATH_NOT_FOUND = 5

FILE_EXTENSION_UNKNOWN = 6

ERROR_EXECUTE_FAILED = 7

SYNCHRONOUS_FAILED = 8

NOT_SUPPORTED_BY_GUI = 9

Others = 10.

5. Converting the hex characters to strings.



CONSTANTS: c_hex (2) TYPE x VALUE '000A'.

DATA: LV_TMP TYPE C.

TRY.

CALL METHOD CL_ABAP_CONV_IN_CE=>UCCP

EXPORTING

UCCP = c_hex

RECEIVING

CHAR = LV_TMP .

CATCH CX_SY_CONVERSION_CODEPAGE.

CATCH CX_PARAMETER_INVALID_TYPE.

CATCH CX_SY_CODEPAGE_CONVERTER_INIT.

ENDTRY.

Note that the hex variable should be of output length 2 for the conversion to take place.

6. Moving contents from one structure to another



Before Unicode:

SRTFDLOM = XSRTFDLOM.

Resolution:

CLASS CL_ABAP_CONTAINER_UTILITIES DEFINITION LOAD.

DATA: LV_TAB(1000) TYPE C.

CALL METHOD CL_ABAP_CONTAINER_UTILITIES=>FILL_CONTAINER_C

EXPORTING

IM_VALUE = XSRTFDLOM

IMPORTING

EX_CONTAINER = LV_TAB

CALL METHOD CL_ABAP_CONTAINER_UTILITIES=>READ_CONTAINER_C

EXPORTING

IM_CONTAINER = LV_TAB

IMPORTING

EX_VALUE = SRTFDLOM.

OR

Before Unicode:

wa_test = upload.

Resolution:

data: cbin TYPE REF TO cl_abap_conv_out_ce,

buffer TYPE xstring,

conv TYPE REF TO cl_abap_conv_in_ce,

view1 TYPE REF TO cl_abap_view_offlen,

view2 TYPE REF TO cl_abap_view_offlen,

view TYPE REF TO cl_abap_view_offlen.

cbin = cl_abap_conv_out_ce=>create( ).

view1 = cl_abap_view_offlen=>create_legacy_view( upload ).

conv = cl_abap_conv_in_ce=>create( ).

iew2 = cl_abap_view_offlen=>create_legacy_view( wa_test ).

Try .

cbin->convert_struc( EXPORTING data = upload view = view1

IMPORTING buffer = buffer).

conv->convert_struc( EXPORTING input = buffer view = view2

IMPORTING data = wa_test ).

CATCH CX_SY_CODEPAGE_CONVERTER_INIT .

CATCH CX_SY_CONVERSION_CODEPAGE .

CATCH CX_PARAMETER_INVALID_TYPE .

CATCH CX_PARAMETER_INVALID_RANGE .

ENDTRY .

Note
that in the above resolution FILL_CONTAINER_C can be used to convert
contents of a structure to string/character array and READ_CONTAINER_C
can be used to move the contents of a string to a structure. Also the
convert class methods can be used in a similar fashion by creating shown
below:

Before Unicode: (from structure to string)

string = wa_structure.

Resolution:

data: cbout TYPE REF TO cl_abap_conv_out_ce,

convt TYPE REF TO cl_abap_conv_in_ce,

buffer TYPE xstring,

view1 TYPE REF TO cl_abap_view_offlen,

view2 TYPE REF TO cl_abap_view_offlen,

view TYPE REF TO cl_abap_view_offlen.

data: begin of t_out,

output(1000) type c,

end of t_out .

cbout = cl_abap_conv_out_ce =>create( ).

view1 = cl_abap_view_offlen =>create_legacy_view( wa_structure ).

convt = cl_abap_conv_in_ce =>create( ).

view2 = cl_abap_view_offlen =>create_legacy_view( t_out ).

TRY.

cbout->convert_struc( EXPORTING data = wa_structure view = view1

IMPORTING buffer = buffer).

convt->convert_struc( EXPORTING input = buffer view = view2

IMPORTING data = t_out ).

CATCH CX_SY_CODEPAGE_CONVERTER_INIT .

CATCH CX_SY_CONVERSION_CODEPAGE .

CATCH CX_PARAMETER_INVALID_TYPE .

CATCH CX_PARAMETER_INVALID_RANGE .

ENDTRY.

string = t_out-output .

Before Unicode: (from string to structure)

wa_structure = string.

Resolution:

data : cbout TYPE REF TO cl_abap_conv_out_ce,

convt TYPE REF TO cl_abap_conv_in_ce,

buffer TYPE xstring,

view1 TYPE REF TO cl_abap_view_offlen,

view2 TYPE REF TO cl_abap_view_offlen,

view TYPE REF TO cl_abap_view_offlen .

data: begin of t_out,

output(1000) type c,

end of t_out .

t_out-output = string.

cbout = cl_abap_conv_out_ce=>create( ).

view1 = cl_abap_view_offlen=>create_legacy_view( t_out ).

convt = cl_abap_conv_in_ce=>create( ).

view2 = cl_abap_view_offlen=>create_legacy_view( wa_structure ).

TRY.

cbout->convert_struc( EXPORTING data = t_out view = view1

IMPORTING buffer = buffer ).

convt->convert_struc( EXPORTING input = buffer view = view2

IMPORTING data = wa_structure ).

CATCH CX_SY_CODEPAGE_CONVERTER_INIT .

CATCH CX_SY_CONVERSION_CODEPAGE .

CATCH CX_PARAMETER_INVALID_TYPE .

CATCH CX_PARAMETER_INVALID_RANGE .

ENDTRY.

7. Split command using TAB character Error:



Before Unicode:

data: separator type x value '09'.

data: str type string.

Split str at separator.

Resolution:

Split str at CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB.

Note that this works only in case of hex value 0009.

8. Error regarding the FORM AUTHORITY_BEGIN (RSAQEXCE)



Before Unicode:

PERFORM AUTHORITY_BEGIN (RSAQEXCE).

PERFORM AUTHORITY (RSAQEXCE) USING 'PLKO'.

PERFORM AUTHORITY (RSAQEXCE) USING 'PLAS'.

PERFORM AUTHORITY(RSAQEXCE) USING 'PLWP'.

PERFORM AUTHORITY(RSAQEXCE) USING 'PLPH'.

PERFORM AUTHORITY(RSAQEXCE) USING 'PLPO'.

PERFORM AUTHORITY_END (RSAQEXCE).

Resolution:

PERFORM AUTHORITY_BEGIN(RSAQEXCE) using 'CL_QUERY_TAB_ACCESS_AUTHORITY'.

PERFORM AUTHORITY(RSAQEXCE) USING 'PLKO' 'CL_QUERY_TAB_ACCESS_AUTHORITY'.

PERFORM AUTHORITY(RSAQEXCE) USING 'PLAS' 'CL_QUERY_TAB_ACCESS_AUTHORITY'.

PERFORM AUTHORITY(RSAQEXCE) USING 'PLWP' 'CL_QUERY_TAB_ACCESS_AUTHORITY'.

PERFORM AUTHORITY(RSAQEXCE) USING 'PLPH' 'CL_QUERY_TAB_ACCESS_AUTHORITY'.

PERFORM AUTHORITY(RSAQEXCE) USING 'PLPO' 'CL_QUERY_TAB_ACCESS_AUTHORITY'.

PERFORM AUTHORITY_END(RSAQEXCE) using 'CL_QUERY_TAB_ACCESS_AUTHORITY'.

This is because the FORM AUTHORITY_BEGIN (RSAQEXCE) has 2 parameters from ECC 6.0 onwards.

9. A Cancel option required in GUI_UPLOAD/GUI_DOWNLOAD Function Module:



Before Unicode:

CALL FUNCTION 'UPLOAD'

EXPORTING

FILENAME = d:/Z400.txt

FILETYPE = 'ASC'

IMPORTING

FILESIZE = l_filesize

CANCEL = cancel

ACT_FILENAME = l_filename

ACT_FILETYPE = l_filetype

TABLES

DATA_TAB = itab

EXCEPTIONS

CONVERSION_ERROR = 1

INVALID_TABLE_WIDTH = 2

INVALID_TYPE = 3

NO_BATCH = 4

UNKNOWN_ERROR = 5

GUI_REFUSE_FILETRANSFER = 6

OTHERS = 7.

Resolution:

CLASS cl_gui_frontend_services DEFINITION LOAD.

DATA: v_title TYPE string,

v_fullpath TYPE string,

filename TYPE string,

v_path TYPE string,

v_user_action TYPE i,

v_encoding TYPE abap_encoding.

MOVE: sy-repid TO v_title.

CALL METHOD cl_gui_frontend_services=>file_save_dialog

EXPORTING

window_title = v_title

with_encoding = 'X'

default_file_name = filename

CHANGING

filename = filename

path = v_path

fullpath = v_fullpath

user_action = v_user_action

file_encoding = v_encoding

EXCEPTIONS

cntl_error = 1

error_no_gui = 2

not_supported_by_gui = 3

OTHERS = 4.

IF sy-subrc <> 0.

EXIT.

ENDIF.

IF v_user_action <> cl_gui_frontend_services=>action_ok.

EXIT.

ENDIF.

CALL FUNCTION 'GUI_DOWNLOAD'

EXPORTING

filename = filename

TABLES

data_tab = itab

EXCEPTIONS

file_write_error = 1

no_batch = 2

gui_refuse_filetransfer = 3

invalid_type = 4

no_authority = 5

unknown_error = 6

header_not_allowed = 7

separator_not_allowed = 8

filesize_not_allowed = 9

header_too_long = 10

dp_error_create = 11

dp_error_send = 12

dp_error_write = 13

unknown_dp_error = 14

access_denied = 15

dp_out_of_memory = 16

disk_full = 17

dp_timeout = 18

file_not_found = 19

dataprovider_exception = 20

control_flush_error = 21

OTHERS = 22.

10. Error regarding Do...Varying clause.



Before Unicode:

DATA: BEGIN OF I_0041_DATE OCCURS 0,

DAR LIKE PA0041-DAR01,

DAT LIKE PA0041-DAT01,

END OF I_0041_DATE.

DO 12 TIMES VARYING I_0041_DATE FROM P0041-DAR01 NEXT P0041-DAR02.

Resolution:

DATA: BEGIN OF I_0041_DATE OCCURS 0,

DAR LIKE PA0041-DAR01,

DAT LIKE PA0041-DAT01,

END OF I_0041_DATE.

DO 10 TIMES VARYING I_0041_DATE-dar FROM P0041-DAR01 NEXT
P0041-DAR02 VARYING I_0041_DATE-dat FROM P0041-DAt01 NEXT P0041-DAt02.

Before Unicode:

DO VARYING ZW_FIELD FROM ZW_FIELD_LINE-F01+1 NEXT ZW_FIELD_LINE-F01+1(1).

Resolution:

DO VARYING ZW_FIELD FROM ZW_FIELD_LINE-F01+1 NEXT ZW_FIELD_LINE-F02+1(1) RANGE ZW_FIELD_LINE-F01.

11. Error regarding the Describe commands



Before Unicode:

Describe field text length len.

Resolution:

Describe field text length len in Character Mode.

12. Error regarding the Function Module POPUP_WITH_TABLE_DISPLAY.



Before Unicode:

POPUP_WITH_TABLE_DISPLAY

Resolution:

POPUP_WITH_TABLE_DISPLAY_OK

13. Error regarding hexadecimal variable comparison



Before Unicode:

data: xf1 type x,

xf2 type x,

xf3 type x.

Concatenate xf1 xf2 to xf3.

If xf1 CS xf3...

Resolution:

data: xf1 type x,

xf2 type x,

xf3 type x.

Concatenate xf1 xf2 to xf3 in byte mode.

If xf1 BYTE-CS xf3...

14. Problems regarding Assignment between Structures



Conversion using includes with group names

Before Unicode:

TYPES: BEGIN OF T_STRUC,

F1 TYPE C,

F2 TYPE C,

F3 TYPE I,

F4 TYPE P,

END OF T_STRUC.

DATA: BEGIN OF STRUC1.

INCLUDE TYPE T_STRUC.

DATA: F5 TYPE X.

DATA: END OF STRUC1.

DATA: BEGIN OF STRUC2.

INCLUDE TYPE T_STRUC.

DATA: F6 TYPE P.

DATA: END OF STRUC2.

STRUC1 = STRUC2. (Unicode error)

In
this case, it was assumed that only the content of the includes is to
be assigned - that is the components F1 to F4. Until now, it was
tolerated that the component F5 is overwritten with a meaningless value.

Resolution:

TYPES: BEGIN OF T_STRUC,

F1 TYPE C,

F2 TYPE C,

F3 TYPE I,

F4 TYPE P,

END OF T_STRUC.

DATA: BEGIN OF STRUC1.

INCLUDE TYPE T_STRUC AS PART1.

DATA: F5 TYPE X.

DATA: END OF STRUC1.

DATA: BEGIN OF STRUC2.

INCLUDE TYPE T_STRUC AS PART1.

DATA: F6 TYPE P.

DATA: END OF STRUC2.

STRUC1-PART1 = STRUC2-PART1.

Conversion Using Offset-Based or Length-Based Accesses

Before Unicode:

DATA: BEGIN OF STRUC1,

F1(10) TYPE C,

F2(20) TYPE C,

F3 TYPE I,

F4 TYPE P,

END OF STRUC1,

BEGIN OF STRUC2,

C1(10) TYPE C,

C2(20) TYPE C,

C3 TYPE X,

C4 TYPE F,

END OF STRUC2.

STRUC1 = STRUC2 (Unicode Error)

In
this example, it is assumed that only the content of the first two
components C1 and C2 is to be passed to F1 and F2, because the following
components F3 and F4 are overwritten by meaningless values.

Resolution

DATA: BEGIN OF STRUC1,

F1 (10) TYPE C,

F2 (20) TYPE C,

F3 TYPE I,

F4 TYPE P,

END OF STRUC1,

BEGIN OF STRUC2,

C1 (10) TYPE C,

C2 (20) TYPE C,

C3 TYPE X,

C4 TYPE F,

END OF STRUC2

STRUC1 (30) = STRUC2 (30).

Since
the initial part of the structures relevant for the assignment is
purely character-type, the operands of the assignment can be selected
using offset-based or length-based accesses. Assignment between
Structure and Single Field of Type N

Before Unicode

DATA: BEGIN OF STRUC,

NUMBER(20) TYPE N,

F2 TYPE P,

END OF STRUC,

NUMBER(20) TYPE N.

NUMBER = STRUC. Unicode error

Assignments between a non-character-type structure and a single field of type N are no longer allowed in Unicode programs

Resolution:

DATA: BEGIN OF STRUC,

NUMBER(20) TYPE N,

F2 TYPE P,

END OF STRUC,

NUMBER(20) TYPE N.

NUMBER = STRUC-NUMBER.

Since
the first component of the structure is to be assigned to the single
field, the code segment can be converted easily by replacing the
assignment of the whole structure with an assignment of the first
structure component. Assignment and Single Field of Type between
Structure D

Before Unicode

DATA: BEGIN OF STRUC,

YEAR (4) TYPE N,

MONTH(2) TYPE N,

DAY(2) TYPE N,

F4 TYPE P,

END OF STRUC,

DATE TYPE D.

DATE = STRUC. (Unicode error)

Assignments between a non-character-type structure and a single field of type D are no longer allowed in Unicode programs.

Resolution:

DATA: BEGIN OF STRUC,

YEAR(4) TYPE N,

MONTH(2) TYPE N,

DAY(2) TYPE N,

F4 TYPE P,

END OF STRUC,

DATE TYPE D.

DATE = STRUC (8).

An
offset-based or length-based access to the character-type initial part
of the structure enables you to convert the problematic code segment to a
Unicode-enabled code.

Source site:https://wiki.sdn.sap.com/wiki/display/Snippets/Common+unicode+errors+and+solutions
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: