一、功能描述
練習ABAP中的Base64編碼/解碼,以及字符編碼轉換
二、相關函數及類接口
1、SCMS_STRING_TO_XSTRING String 轉 Xstring (按照指定字符編碼)
2、SCMS_BASE64_ENCODE_STR Base64 編碼
3、SCMS_BASE64_DECODE_STR Base64 解碼
4、SCP_CODEPAGE_BY_EXTERNAL_NAME 獲得Codepage
5、cl_abap_conv_in_ce Xstring轉String (按照指定字符編碼)
三、練習代碼
*&---------------------------------------------------------------------* *& Report ZQBTEST_15 *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT zqbtest_15. PARAMETERS: p_instr TYPE string, p_code TYPE c LENGTH 128 DEFAULT 'GBK'. DATA: instr TYPE string, xstr TYPE xstring, encode_str TYPE string, charset TYPE string, mimetype(128) TYPE c. instr = p_instr. WRITE : / ' Instr:' ,instr. charset = p_code. CONCATENATE '"text/html; charset=' p_code '"' INTO mimetype. CALL FUNCTION 'SCMS_STRING_TO_XSTRING' EXPORTING text = instr mimetype = mimetype IMPORTING buffer = xstr EXCEPTIONS failed = 1 OTHERS = 2. IF sy-subrc <> 0. * Implement suitable error handling here ENDIF. WRITE : / ' Xstr:' ,xstr. CALL FUNCTION 'SCMS_BASE64_ENCODE_STR' EXPORTING input = xstr IMPORTING output = encode_str. WRITE : / 'Encode:' ,encode_str. * 2018.08.18 若http用Get方式作為URL一部分需要將+替換成%2B DATA: out TYPE string . out = encode_str. REPLACE ALL OCCURRENCES OF '+' IN out WITH '%2B'. WRITE: / ' Out:', out. * 2018.08.18 end CLEAR xstr. CALL FUNCTION 'SCMS_BASE64_DECODE_STR' EXPORTING input = encode_str * unescape = 'X' IMPORTING output = xstr EXCEPTIONS failed = 1 OTHERS = 2. IF sy-subrc <> 0. * Implement suitable error handling here ENDIF. WRITE : / 'Decode:' ,xstr. DATA: codepage(4) TYPE n. DATA: encoding(20) TYPE c. DATA: convin TYPE REF TO cl_abap_conv_in_ce. DATA: str TYPE string. CALL FUNCTION 'SCP_CODEPAGE_BY_EXTERNAL_NAME' EXPORTING external_name = charset IMPORTING sap_codepage = codepage EXCEPTIONS not_found = 1 OTHERS = 2. IF sy-subrc <> 0. * Implement suitable error handling here ENDIF. encoding = codepage. "創建解碼對象 convin = cl_abap_conv_in_ce=>create( encoding = encoding input = xstr ). CALL METHOD convin->read( IMPORTING data = str ). WRITE: / 'Encode:' ,str.
四、執行結果
輸入
輸出