gpt4 book ai didi

xml - 将包含 XML 文本的 base64 解码为 String 变量

转载 作者:数据小太阳 更新时间:2023-10-29 03:00:01 25 4
gpt4 key购买 nike

我无法解码以 base64 格式提供的 XML,字符串变量 (lv_string) 显示一些不可读的内容。

例子:

Ü-ÅïÎ#è-´ï®¹ïn÷ëÞ#èMÃÛmõØMôÛm´ë##ë~#ë#½èN»Ü=¶ãÞwà]#ßÍüß#ýØ=õÛm´ï~øë^#ë#µè.#èN¹Ü=¶ïÞ¹ï

代码如下:

Data: lt_content    Type standard table of x255,
lv_xstring Type xstring,
lv_string Type string,
encod Type Abap_encoding Value 4110.

Select Single xml_dte into @Data(xml_b64)
From zmmvf_edocdet
Where numinterno = '0000000012'.

IF Sy-subrc Eq 0.

Call function 'SCMS_BASE64_DECODE_STR'
Exporting
Input = xml_b64
Importing
Output = lv_xstring
Exceptions
Failed = 1
Others = 2.

If Sy-subrc Eq 0.

Data(lv_len) = xstrlen( lv_xstring ).
Call function 'SCMS_XSTRING_TO_BINARY'
Exporting
buffer = lv_xstring
Importing
output_length = lv_len
Tables
binary_tab = lt_content[].

Call function 'SCMS_BINARY_TO_STRING'
Exporting
input_length = lv_len
* encoding = encod
Importing
text_buffer = lv_string
Tables
binary_tab = lt_content[]
Exceptions
failed = 1
Others = 2.
ENDIF.
ENDIF.

最佳答案

我以前也遇到过这个问题。您可以使用以下内容

data:
lv_base64 type string,
lv_xstring type xstring,
lv_output type string.

* example base64 string
lv_base64 = 'PGh0bWw+PGhlYWQ+PHRpdGxlPlRpdGxlPC90aXRsZT48L2hlYWQ+PGJvZHk+PHA+SGVsbG8gV29ybGQ8L3A+PC9ib2R5PjwvaHRtbD4='.

* convert base64 to binary (xstring)
call function 'SCMS_BASE64_DECODE_STR'
exporting
input = lv_base64
importing
output = lv_xstring
exceptions
failed = 1
others = 2.

* use codepage conversion to convert xstring to string (UTF-8)
* catch possible conversion errors
try.

lv_output = cl_abap_codepage=>convert_from( source = lv_xstring ).

catch cx_parameter_invalid_range .
catch cx_sy_codepage_converter_init .
catch cx_sy_conversion_codepage .
catch cx_parameter_invalid_type .
endtry.

write lv_output.

结果是下面的输出

<html><head><title>Title</title></head><body><p>Hello World</p></body></html>

您可以使用其他参数控制转换,例如:

如果你喜欢不同的代码页

如果你想用一个特殊的字符替换不可见的字符

如果你想忽略转换错误

lv_output = cl_abap_codepage=>convert_from(
source = lv_xstring
codepage = <your favourite codepage here>
replacement = <conversion char for not convertible chars>
ignore_cerr = <pass 'X' to ignore conversion errors>
).

关于xml - 将包含 XML 文本的 base64 解码为 String 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55190490/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com