系统是老R3,非Unicdoe系统,某些表字段是从外界系统过来的,由于接口设计的固定长度,外界系统传超长字符串过来后,就可能从最后一个中文字符中间截断,这问题到还没什么,只不过显示时最后一个字符显示成乱码而已,但是,如果将这些表字段捞出来与其它分隔符(如竖线) CONCATENATE时,可能会将这个分隔符吃掉,导致这些数据抛到对方系统后,无法再分隔,还原成一个个字段
如下面运行过程中:c3本身是一个 司 字后面跟半个中文字符,当使用 竖线分隔符与0字符CONCATENATE后,发现竖线没有了,它与前面的?问号(注:不是真正的问号,而是由于编码B5在GBK字符集里找不到,ABAP编辑器以问号显示而已)合成 祙 字了,即竖线被前面的半个汉字编码给吃掉了:
下面是经过frm_trunct_last 方法将半个中文字符截断后,再CONCATENATE拼接时,分隔符竖线就不会被吃掉了:
DATA: str TYPE string.
DATA: x3(3) TYPE x.
DATA: c3(3) TYPE c.
FIELD-SYMBOLS:<c3> TYPE x.
START-OF-SELECTION.
ASSIGN c3 TO <c3> CASTING.
"CBBE为“司”的编码,B5为前半个汉字编码,如“祙、单”等字的前半个就是B5
<c3> = 'CBBEB5'."模拟半个中文
WRITE:/ c3.
str = c3.
CONCATENATE str `0` INTO str SEPARATED BY `|`.
WRITE:/ str.
str = c3.
PERFORM frm_trunct_last USING str .
CONCATENATE str `0` INTO str SEPARATED BY `|`.
WRITE:/ str.
*&---------------------------------------------------------------------*
*& Form frm_trunct_last
*&---------------------------------------------------------------------*
* 如果字符串最后一个是半个字符,则截掉
*----------------------------------------------------------------------*
* -->STR 如果不会吃字符,则返回原字符串
*----------------------------------------------------------------------*
FORM frm_trunct_last USING str TYPE string .
DATA: xstr TYPE xstring.
DATA: l_codepage(4) TYPE n .
DATA: l_encoding(20).
DATA: truc_str TYPE string.
DATA: off_index TYPE i.
DATA: trunct_str TYPE string.
DATA: last_char TYPE string.
DATA: last_char_code TYPE xstring.
DATA: len TYPE i.
DATA: x7f TYPE x VALUE '7F'.
DATA: convout TYPE REF TO cl_abap_conv_out_ce.
DATA: convin TYPE REF TO cl_abap_conv_in_ce.
CALL FUNCTION 'SCP_CODEPAGE_BY_EXTERNAL_NAME'
EXPORTING
external_name = 'UTF-16BE'
IMPORTING
sap_codepage = l_codepage.
l_encoding = l_codepage.
convout = cl_abap_conv_out_ce=>create( encoding = l_encoding ).
convout->write( data = str ).
xstr = convout->get_buffer( ).
truc_str = xstr.
len = STRLEN( truc_str ).
len = len / 4 .
off_index = 4 * ( len - 1 ).
xstr = truc_str+0(off_index).
convin = cl_abap_conv_in_ce=>create( encoding = l_encoding input = xstr ).
CALL METHOD convin->read
IMPORTING
data = trunct_str.
off_index = 4 * ( len - 1 ).
xstr = truc_str+off_index(4).
convin = cl_abap_conv_in_ce=>create( encoding = l_encoding input = xstr ).
CALL METHOD convin->read
IMPORTING
data = last_char.
CALL FUNCTION 'SCP_CODEPAGE_BY_EXTERNAL_NAME'
EXPORTING
external_name = 'GBK'
IMPORTING
sap_codepage = l_codepage.
l_encoding = l_codepage.
convout = cl_abap_conv_out_ce=>create( encoding = l_encoding ).
convout->write( data = last_char ).
last_char_code = convout->get_buffer( ).
len = XSTRLEN( last_char_code ).
IF len = 1 AND last_char_code > x7f.
str = trunct_str.
ENDIF.
ENDFORM. "frm_trunct_last
注:分隔符是否被吃掉了,在屏幕输出上是看不出来的,如下面是输出结果:
但从上面的调试过程可以看出是被吃掉了,经测试传到其他系统后,也是会被吃掉的