本文共 2242 字,大约阅读时间需要 7 分钟。
一、功能描述
练习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 hereENDIF.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一部分需要将+替换成%2BDATA: out TYPE string .out = encode_str.REPLACE ALL OCCURRENCES OF '+' IN out WITH '%2B'.WRITE: / ' Out:', out.* 2018.08.18 endCLEAR 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 hereENDIF.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 hereENDIF.encoding = codepage."创建解码对象convin = cl_abap_conv_in_ce=>create( encoding = encoding input = xstr ).CALL METHOD convin->read( IMPORTING data = str ).WRITE: / 'Encode:' ,str.
四、执行结果
转载地址:http://pphaf.baihongyu.com/