老R3(非Unicode系统),发现读取出来的数据中有编码为0的字符,存文件后发现串行,所以需要先替换
DATA: str TYPE string.
*str = zacct_12-txt+47.
str = '南京人力资源部'.
"C4CFBEA9C8CBC1A6D7CAD4B4B2BF
DATA:c(17).
FIELD-SYMBOLS: TYPE x .
ASSIGN c TO CASTING.
WRITE:/ c,.
str = c.
PERFORM replace_null USING str.
*&---------------------------------------------------------------------*
*& Form replace_null
*&---------------------------------------------------------------------*
* text R3系统中替换字符串中的空字符(编码为0的)
*----------------------------------------------------------------------*
* -->P_STR text
*----------------------------------------------------------------------*
FORM replace_null USING p_str TYPE string.
******************************编
DATA: xstr TYPE xstring.
DATA: l_codepage(4) TYPE n .
DATA: l_encoding(20).
CALL FUNCTION 'SCP_CODEPAGE_BY_EXTERNAL_NAME'
EXPORTING
external_name = 'UTF-16BE'"不管中文还是英文,先统一编码,都转成两字节的
IMPORTING
sap_codepage = l_codepage.
l_encoding = l_codepage.
DATA: convout TYPE REF TO cl_abap_conv_out_ce.
convout = cl_abap_conv_out_ce=>create( encoding = l_encoding ).
convout->write( data = p_str ).
xstr = convout->get_buffer( ).
******************************替
DATA:truc_str TYPE string,
str_len TYPE i.
truc_str = xstr.
str_len = STRLEN( truc_str ).
str_len = str_len / 4 .
DATA: cnt TYPE i.
DATA: c4(4) .
DATA: x2(2) TYPE x.
DATA: i TYPE i.
DATA: xstr2 TYPE xstring.
DATA: off_index TYPE i.
DO str_len TIMES.
off_index = cnt * 4.
c4 = truc_str+off_index(4).
x2 = c4.
i = x2.
IF i = 0."如果发现是空字符,使用空格替换
x2 = '0020'.
CONCATENATE xstr2 x2 INTO xstr2 IN BYTE MODE.
ELSE.
CONCATENATE xstr2 x2 INTO xstr2 IN BYTE MODE.
ENDIF.
WRITE: / c4,x2,i,xstr2.
cnt = cnt + 1.
ENDDO.
******************************解
DATA: convin TYPE REF TO cl_abap_conv_in_ce.
convin = cl_abap_conv_in_ce=>create( encoding = l_encoding input = xstr2 ).
DATA: str TYPE string.
CALL METHOD convin->read
IMPORTING
data = p_str.
WRITE: / p_str.
ENDFORM. "replace_null