概述
一、功能描述
练习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.
四、执行结果
最后
以上就是儒雅黑猫为你收集整理的【转】ABAP BASE64编码与解码练习的全部内容,希望文章能够帮你解决【转】ABAP BASE64编码与解码练习所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复