3

I can't decode an XML provided in base64 format, the string variable (lv_string) shows something unreadable.

Example:

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

Here is the code:

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.
1
  • I just tested your code, by setting xml_b64 to a fixed text encoded in UTF-8 then in base64, and your code successfully decodes it. So the base64 from your database does not hold a UTF-8 text. Maybe it already contains junk bytes. Please post the 100 first characters of your base64 here if you wan't us to tell you (no more an ABAP question though). Commented Mar 16, 2019 at 12:10

2 Answers 2

5

I also had this problem in the past. You can use the following

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.

The result is the following output

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

You can control the conversion with additional parameters, for example:

if you prefer to have a different codepage

if you want to replace not visible characters with a special char

if you want to ignore conversion errors

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>
).
Sign up to request clarification or add additional context in comments.

3 Comments

I changed my code exactly as you but shows the same unreadable string.
try. lv_string = cl_abap_codepage=>convert_from( source = lv_xstring codepage = 'ISO-8859-1' ignore_cerr = 'X'). catch cx_parameter_invalid_range . catch cx_sy_codepage_converter_init . catch cx_sy_conversion_codepage . catch cx_parameter_invalid_type . endtry.
is it possible the input base64 is corrupt? Also: if you remove ignore_cerr, do you get an exception? I tried with ISO-8859-1 and the string is correct
0

A short solution, if we assume that the base64 string contains the XML in the UTF-8 encoding, is to use the method DECODE_BASE64 of class CL_HTTP_UTILITY:

lv_string = cl_http_utility=>if_http_utility~decode_base64( xml_b64 ).

Minimal, Complete and Verifiable Example :

(please allow me to reuse the good example of @manuel_b)

DATA(xml_b64) = `PGh0bWw+PGhlYWQ+PHRpdGxlPlRpdGxlPC90aXRsZT48L2hlYWQ+`
             && `PGJvZHk+PHA+SGVsbG8gV29ybGQ8L3A+PC9ib2R5PjwvaHRtbD4=`.

DATA(lv_string) = cl_http_utility=>if_http_utility~decode_base64( xml_b64 ).

ASSERT lv_string = `<html><head><title>Title</title></head><body><p>Hello World</p></body></html>`.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.