替换空字符NULL(字符编码为0的字符)
老R3(非Unicode系统),发现读取出来的数据中有编码为0的字符,存文件后发现串行,所以需要先替换
DATA: str TYPE string.
*str = zacct_12-txt+47.
str = '南京人力资源部'.
"C4CFBEA9C8CBC1A6D7CAD4B4B2BF
DATA:c(17).
FIELD-SYMBOLS: <c> TYPE x .
ASSIGN c TO <c> CASTING.
<c> = '00C4CFBEA9C8CB00C1A6D7CAD4B4B2BF00'."在 南京人力资源部 前中后加上了空字符00,方便测试
WRITE:/ c,<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
原文出自 江正军 技术博客,博客链接:www.cnblogs.com/jiangzhengjun