中文字元擷取亂碼問題
阿新 • • 發佈:2018-12-27
又是非Unicode老系統問題,當有中文時,擷取就成問題,原因是中文佔兩位元組,而英文只佔一個位元組,而在老R3系統中C型別只佔一個位元組。思路:將字元統一轉換成Unicode後擷取DATA: s TYPE string.s = '1a的的的b1才才才餓餓餓d1是是是e1哦哦哦'.WRITE:/ s .WRITE:/ s+14(5)."直接取會出亂碼!!PERFORM frm_substring USING s s 14 5."從第15個字元開始取,取5個字元WRITE: / s.*&---------------------------------------------------------------------**& Form frm_substring*&---------------------------------------------------------------------* * 老R3(非Unicode系統)字串截*----------------------------------------------------------------------** -->P_SRC text 原字串* -->P_SUB text 擷取後的字串* -->P_off text 起始索引(從0開始)* -->P_LEN text 擷取長度*----------------------------------------------------------------------*FORM frm_substring USING p_src p_sub p_off p_len. 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_src )."編碼 xstr = convout->get_buffer( ). WRITE: / xstr." DATA:truc_str TYPE string,off TYPE i,len TYPE i. truc_str = xstr. off = p_off * 4. len = p_len * 4. truc_str = truc_str+off(len). WRITE / truc_str."擷取獲得的二進串 DATA: rf_elem_typ TYPE REF TO cl_abap_elemdescr. DATA: rf_c TYPE REF TO data. FIELD-SYMBOLS: <fs_c> TYPE x . DATA: c_len TYPE i. c_len = 2 * p_len."Unicode字元,每個佔2個位元組 rf_elem_typ ?= cl_abap_elemdescr=>get_c( c_len ). CREATE DATA rf_c TYPE HANDLE rf_elem_typ. ASSIGN rf_c->* TO <fs_c> CASTING. <fs_c> = truc_str. DATA: convin TYPE REF TO cl_abap_conv_in_ce. convin = cl_abap_conv_in_ce=>create( encoding = l_encoding input = <fs_c> ). DATA: str TYPE string. CALL METHOD convin->read"解碼 IMPORTING data = p_sub.ENDFORM. " frm_substring