Decoding BASE64 in ABAP

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/champaignwolf/article/details/86609005

There are routines proposed below, first one is to convert base64 to a string, and second one to a xstring.
Third one was mentioned by Sandra in comments section.

A small form routine to decode base64 (TYPE STRING) into plaintext (TYPE STRING) :

Error rendering macro 'code': Invalid value specified for parameter 'lang'

FORM decode_base64
    USING
      base64     TYPE string,
    CHANGING
      plaintext  TYPE string.

  CHECK base64 IS NOT INITIAL.

  CONSTANTS:
    lc_op_dec TYPE x VALUE 37.
  DATA:
    l_xstr TYPE xstring,
    lr_conv TYPE REF TO cl_abap_conv_in_ce.

  CALL 'SSF_ABAP_SERVICE'
    ID 'OPCODE' FIELD   lc_op_dec
    ID 'BINDATA' FIELD   l_xstr
    ID 'B64DATA' FIELD   base64. "#EC CI_CCALL

  TRY.

      lr_conv = cl_abap_conv_in_ce=>create( input = l_xstr ).
      lr_conv->read( IMPORTING data = plaintext ).

    CATCH cx_sy_conversion_codepage.
      CLEAR plaintext.
      MESSAGE i999(samx) WITH text-004 text-005.
  ENDTRY.

ENDFORM.

A small form routine to decode base64 (TYPE STRING) into xstring (TYPE XSTRING) :

Error rendering macro 'code': Invalid value specified for parameter 'lang'

FORM decode_base64_to_xstring
      USING
        i_base64   TYPE string
      CHANGING
        e_xstring  TYPE xstring.

  CALL FUNCTION 'SSFC_BASE64_DECODE'
    EXPORTING
      b64data = i_base64
    IMPORTING
      bindata = e_xstring
    EXCEPTIONS
      OTHERS  = 8.

ENDFORM.

Conversion from base64 to string may also be achieved by this small code :

Error rendering macro 'code': Invalid value specified for parameter 'lang'

CALL METHOD cl_http_utility=>if_http_utility~decode_base64
    EXPORTING
      encoded = l_string_base64
    RECEIVING
      decoded = l_string.

猜你喜欢

转载自blog.csdn.net/champaignwolf/article/details/86609005