SAP系统与其他系统交互时,经常会遇到字符编码不同,转换时出现乱码的问题。一般可以将数据转换为二进制形式进行交换来避免这种错误。下面是SAP系统与第三方系统交互时的实例,SAP系统的charset为UTF-16(对应的codepage 4102),第三方系统的charset是GBK(对应的codepage 8400).
SAP发送数据时的处理,将数据转换为二进制数据发送:
DATA:lstr_tmp TYPE xstring.
DATA:lv_mimetype TYPE char64
VALUE 'APPLICATION/XML;charset=gbk'.
* 将待发送的内容转换为二进制编码
CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
EXPORTING
text = i_datain "string
mimetype = lv_mimetype
IMPORTING
buffer = lstr_tmp
EXCEPTIONS
failed = 1
OTHERS = 2.
IF sy-subrc NE 0.
gs_msg-msgtyp = 'E'.
gs_msg-msgno = '125'.
gs_msg-msgtxt = ls_msg.
CONCATENATE 'Stg1:' '数据转换错误'
INTO gs_msg-msgtxt.
APPEND gs_msg TO c_msgout.
ENDIF.
* Sets the HTTP body of this entity to the given binary data
lo_client->request->set_data( EXPORTING data = lstr_tmp
offset = 0 ).
SAP接收第三方系统数据时,以二进制数据接收:
DATA:ls_xstr TYPE xstring.
DATA:l_r_converter TYPE REF TO cl_abap_conv_in_ce.
* Returns the HTTP body of this entity as binary data
ls_xstr = lo_client->response->get_data( ). "返回二进制数据
TRY.
CALL METHOD cl_abap_conv_in_ce=>create
EXPORTING
encoding = '8400'
* endian = 'B'
* replacement = '#'
* ignore_cerr = ABAP_FALSE
input = ls_xstr
RECEIVING
conv = l_r_converter
.
CALL METHOD l_r_converter->read
IMPORTING
data = ls_retstr.
CATCH cx_sy_codepage_converter_init.
* 处理错误
CATCH cx_root.
* 处理错误
ENDTRY.
阅读(1947) | 评论(4) | 转发(0) |