Base64 Encoding and Decoding in SAP ABAP

In this blog post we will discuss Base64 algorithm, ABAP Function Modules which can be used to Encode and Decode Base64, ABAP program to Encode text to Base64 or Decode Base64 format to Text. There are several ABAP Functional Modules we can use when it comes to Encoding and Decoding Base64. Let’s first look at the Base64 algorithm and its functionalities. Then, at the list of SAP Function Modules that can be used for Base64 conversions. Finally, we will learn how to create an ABAP program in SAP to Encode and Decode Base64. Moreover, we’ll discuss the use of Base64 in real life integration scenarios in SAP.

SAP Versions used in the illustration:

  • SAP S4 HANA Fashion 1709

Introduction to Base64 Algorithm.

Base64 schema is a text representation of Binary data. It is mainly used to transmit large files or data over the web or between applications without data being corrupted.

For example, Base64 representation of sentence,

Welcome to SAP Integration Hub.‘ is

V2VsY29tZSB0byBTQVAgSW50ZWdyYXRpb24gSHViLg==‘.

Base64 Encoding Algorithm Flow Overview.

Base64 algorithm flow diagram. How Base64 Encoding algorithm works.
Base64 Encoding algorithm flow diagram.

ASCII code represents data in 8 Bits. Base64 algorithm first groups Bits array of ASCII code into 4 groups of 6 bits each. Then each group of 6 bits is again mapped to Base64 code. The name “Base64” comes from the groups of 6 bits each. 2 to the power 6 is 64, hence the name Base64.

 

Illustration of the Base64 Encoding Algorithm

Text ‘SAP’ in Base64 is represented as ‘U0FQ’. Let’s look at how algorithm converts the text into the Base64 format.

Input text is,

SAP‘.

Text ‘SAP‘ in ASCII code is represented by,

83 65 80‘.

Binary representation of the ASCII code above is,

01010011 01000001 01010000‘.

Now group the complete bit array to groups of 6 bits.

010100|110100|000101|010000

Now convert each group of 6 bits to numeric.

20 | 52 |5 |16

Map each numeric value to Base64 character using the Encoding table below. 20 is ‘U’, 52 is ‘0’, etc. Full representation of 20 | 52 |5 |16 is,

U|0|F|Q

While Encoding happens from the top down as shown in the example above, Decoding happens from the bottom up. You can use this tool to Encode and this one to decode online.


Use Cases of Base64 in SAP Integration Scenarios.

Base64 is a great way to transfer XML, PDF, Text, and Image files among SAP applications, web services, and SAP APIs. When APIs don’t have capabilities to transfer large files as attachments, use Base64 Encoded string to transfer data between systems.

In outbound scenarios, use this method to transfer Files generated in SAP application server (AL11) between AIF (Application Integration Framework) and SAP Process Orchestration (PI/PO) using ABAP Proxies. Convert the Files generated by SAP to Base64 and assign the Base64 value to ABAP Proxy message. Implement a Java Mapping in PI/PO to decode the Base64 string back to text format. For example, Payment Data XMLs generated by SAP standard Payment Run (F110) can be transferred to PI/PO using an ABAP proxy in Base64 format.

For inbound scenarios, use the Base64 decoding method to process files imported to SAP via web services or PI/PO. You can directly process this data in SAP ABAP programs without saving them in AL11. Or, if the Files should be processed by SAP standard programs, save the file in AL11 and call the SAP standard program using ABAP key word SUBMIT. For example, Electronic Bank Statements which are transferred from Banks through PI/PO can then be sent to SAP through ABAP proxy.


ABAP Function Modules to Encode Text to Base64.

Let’s encode String to Base64 format using ABAP Function Modules ‘SCMS_STRING_TO_XSTRING’ and ‘ SCMS_BASE64_ENCODE_STR’.

The first step is to convert the string (text) to XString. XSTRING is predefined byte-like ABAP type with variable length which holds the value in Hexdecimal format. Usually Xstring type is used when we need to transfer heavy files such as XMLs, PDFs, etc. between SAP ABAP and Java applications.

Step 1: Use Functional Module SCMS_STRING_TO_XSTRING to Convert String to Xstring.

Step 2: Use FM ‘SCMS_BASE64_ENCODE_STR’ to Convert the Xstring to Base64.


ABAP Function Modules to Decode Base64 to String.

Decoding of the Base64 format to String is done in three steps. First, we will convert the Base64 format to XString using ‘SCMS_BASE64_DECODE_STR’. Then, XString is converted to Binary using Function Module ‘SCMS_XSTRING_TO_BINARY’. Finally, Binary value is decoded to actual Text or String value using Function Module ‘SCMS_BINARY_TO_STRING’.

Step 1: Convert Base64 String XString Using Function Module ‘SCMS_BASE64_DECODE_STR’.

Step 2: Convert Xstring to Binary ‘SCMS_XSTRING_TO_BINARY’.

Table’s parameter ‘binary_tab’ outputs the Xstring in Binary format.

Step 3: Decode the Binary to Text (String) Format Using FM ‘SCMS_BINARY_TO_STRING’.

Exporting parameter ‘text_buffer’ of the Functional Module ‘SCMS_BINARY_TO_STRING’ outputs the decoded text value.


ABAP Program Encode and Decode Base64 format.

The program has three selection screen parameters, one input parameter to enter either the Text that should be Encoded or Base64 string which should be decoded with options to select either Encode or Decode.

Encoder and Decoder program for SAP ABAP.
SAP ABAP Base64 Encoder and Decoder.

Select either Decode or Encode functions, depending on your input string.

Encoded or Decoded output is written into output screen.

SAP ABAP Encoder and Decoder Program output screen.
Encoder and Decoder output

ABAP Program Code for Base64 Conversion using FMs.


Encode and Decode Base64 using ABAP Class CL_HTTP_UTILITY.

We can also use HTTP utility class CL_HTTP_UTILITY. This utility class contain methods to directly encode or decode Base64 string.

Decode Base64 using method DECODE_BASE64.

Using static method DECODE_BASE64 decode Base64 string directly to string.

Encode Base64 using method ENCODE_BASE64.

Static method ENCODE_BASE64 allow us to encode string to Base64.

 


ABAP Program to Encode and Decode Base64 using CL_HTTP_UTILITY Class.

I have re-designed the ABAP program to convert Base64 using CL_HTTP_UTILITY class methods ENCODE_BASE64 and DECODE_BASE64 .

 


Other Related ABAP Functions to Use with Base64 Function Modules.

In order to convert File to Base64 format, first, you need to read the files from application server (AL11). Read data from AL11 using OPEN DATASET,
READ DATASET, and CLOSE DATASET key words.


I hope you learned how Base64 representation works, and the use cases of Base64. Encoding and Decoding of Base64 in SAP can be done using Function Modules or utility class CL_HTTP_UTILITY. If you have any questions on Base64 topics covered by the article, please leave a comment below.

Share with your colleagues!

Get the latest tips and tricks on SAP!

5 Comments

Add a Comment

Your email address will not be published. Required fields are marked *

Get the latest tips and tricks on SAP!