Skip to Content
Technical Articles

Base64 Function Modules in SAP ABAP

The base64 algorithm is a secure and efficient way to transfer data via the Internet. In SAP, you can use Base64 when you are implementing file-based interfaces between your SAP system and third-party applications. Instead of sending the file as an attachment in a message you can send file data as a Base64 string. For example, for outbound interfaces, you first need to encode the file in Base64 format and transmit it to the target system as a Base64 string. In the target system, you need to decode the Base64 string back to actual data.

This method comes in handy if you want to implement file-based interfaces via Application Integration Framework (AIF) as AIF is not able to handle file attachments.

 

This article goes into detail on Function Modules (FM) in SAP that we can use to encode and decode Base64 string. Use encoding FMs for outbound interface messages and decoding FMs for inbound interface messages.

Function Modules for Base64 Encoding:

 

There are two function modules to encode a string to Base64 format. First, convert string to Xstring using ‘SCMS_STRING_TO_XSTRING‘. Then use ‘SCMS_BASE64_ENCODE_STR‘ to convert Xstring to Base64.

Function Modules for Base64 Decoding:

 

Decoding is a three-step procedure. Convert Base64 string to Xstring using ‘SCMS_BASE64_DECODE_STR‘. Next, transform the Xstring to Binary using function module ‘SCMS_XSTRING_TO_BINARY‘. Finally, turn the Binary format to String via ‘SCMS_BINARY_TO_STRING‘.

How to Decode and Encode Base64 in SAP PI/PO:

 

In most instances, you will use SAP Process Orchestration (PO) to interface between SAP and third-party applications. In these instances, we can use mapping techniques such as User Defined Functions (UDFs), Java Mappings or Adapter Modules to decode and encode Base64.

 

To summarize, you can use Base64 string to transfer data such as files, PDFs, images, etc. between SAP and external APIs. Using this method you can avoid sending files as attachments in the message payload. Use the FMs listed above to Encode and Decode Base64 in your SAP application or interface program.

You can learn more about the Base64 algorithm, use cases of Base64 and a sample ABAP program to decode/encode Base6 here.

Thank you for reading, hope this information will help you design interfaces.

 

Cheers!

7 Comments
You must be Logged on to comment or reply to a post.
  • Hello,
    i would like to add that there are also BASE64 methods in CL_HTTP_UTILITY class:

    DECODE_BASE64
    ENCODE_BASE64
    DECODE_X_BASE64
    ENCODE_X_BASE64

    And I personally prefer to use this class instead of (unreleased) functions.

  • “secure and efficient”, can you elaborate on this part? From a security perspective, the data is not encrypted or checksummed, for efficiency, base64 encoded data is typically larger than the original.

  • If you talk about SCMS_STRING_TO_XSTRING then you may complete that it encodes the string in UTF-8 by default. Idem for the decoding, you convert from a xstring text in UTF-8. Note that instead of all those old function modules, you may use CL_ABAP_CODEPAGE (ABAP 7.02 to 7.52), that’s much shorter:

    DATA: xstr TYPE xstring,
          str  TYPE string.
    TRY.
    
    str = cl_abap_codepage=>convert_from( source = xstr ). " default is UTF-8
    
    xstr = cl_abap_codepage=>convert_to( source = str ). " default is UTF-8
    
    CATCH cx_root.
      " handle conversion errors
    ENDTRY.