Guru: Decoding Base64 ASCII
August 18, 2025 Chris Ringer
A co-worker of mine recently asked me how to decode a base64 ASCII string in RPG, received from an HTTP response. My response was “that’s easy”, simply use the relevant SQL function BASE64_DECODE. I promptly wrote some sample code and after some debugging I realized this function threw me a curveball.
Let me demonstrate the issue and offer a simple solution.
01 **Free 02 ctl-opt DftActGrp(*No) Copyright('(C) Chris Ringer'); 03 dcl-s string_UTF8 varchar(1000) ccsid(*UTF8); 04 dcl-s base64_UTF8 varchar(1350) ccsid(*UTF8); 05 dcl-s decoded_UTF8 varchar(1000) ccsid(*UTF8); 06 dcl-s decoded_Hex varchar(1000) ccsid(*HEX); 07 Exec SQL Set Option Commit=*NONE, Naming=*SYS; 08 string_UTF8 = '{"alg":"RS256","typ":"JWT"}'; 09 exec sql set :base64_UTF8 = BASE64_ENCODE(:string_UTF8); 10 decoded_UTF8 = ''; 11 exec sql set :decoded_UTF8 = BASE64_DECODE(:base64_UTF8); 12 decoded_Hex = ''; 13 exec sql set :decoded_Hex = BASE64_DECODE(:base64_UTF8); 14 decoded_UTF8 = decoded_Hex; 15 *InLR = *On; 16 Return;
Example 1
Lines 01 and 02: This coding example is fully free-form code and includes some compile options.
Line 03: The plain ASCII string to encode into base64. *UTF8 is a predefined constant for CCSID 1208.
Line 04: The ASCII string encoded as base64.
Line 05: The same plain ASCII string as Line 03 now decoded from base64.
Line 06: The plain ASCII string but in hex (raw binary) format.
Line 07: Add your favorite SQL pre-compiler options here.
Lines 08 and 09: This tech tip is about decoding base64 but we need to first encode the string into base64 using the SQL function BASE64_ENCODE. This string is a simple JWT header. For a base64 refresher, go HERE.
Line 10: Initialize variable to an empty string.
Line 11: Attempts to decode the base64 ASCII back into a plain text ASCII variable using the SQL function BASE64_DECODE but this code surprisingly fails. In debug, the SQLSTATE value is 42806: “A value cannot be assigned to a variable, because the data types are not compatible.”
Here is the SQL pre-compiler code (Figure 1).

Figure 1
And examining the SQL variables reveals they are CCSID *HEX (figure 2).

Figure 2
Lines 12 and 13: Since variables SQL_00012 and SQL_00013 are CCSID *HEX, I decided to assign the result to a *HEX variable and examine the result in debug. If you compare the bytes (figure 3) to an ASCII table, clearly the decoded value is ASCII and looks correct.
EVAL decoded_Hex:x

Figure 3
Line 14: Assign that *HEX variable to the ASCII variable. Success! The decoded value (Figure 4) matches the original value (example 1 line 08). You may delete unneeded lines 10 and 11 in the example code.
EVAL decoded_UTF8

Figure 4
Lines 15 and 16: Exit the program.
You may have noticed all the non-HEX variables are ASCII. None are EBCDIC (CCSID 37 in the USA). This is advantageous because mixing and converting between ASCII and EBCDIC may lead to data loss because the characters in the two character sets do not have 100 percent overlap.
I hope this tip is helpful for you. Until next time, keep coding!
Chris Ringer began coding RPG programs in 1989, and after a recent unexpected but valuable detour to C# is happy to be back in the IBM world. In his spare time he enjoys cycling and running – and taking the family dog Eddie for walks.
RELATED STORIES
Guru: A Faster Way To Sign A JWT
Guru: Web Concepts For The RPG Developer, Part 4
Guru: Web Concepts For The RPG Developer, Part 3
Guru: Web Concepts For The RPG Developer, Part 2
Guru: Web Concepts For The RPG Developer, Part 1
Guru: The PHP Path To Victory, Part 1