• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • 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

    PHP Crash Course For RPG Developers

    Installing PHP on Your Laptop

    Debugging Server Jobs In Green Screen

    Share this:

    • Share on Reddit (Opens in new window) Reddit
    • Share on Facebook (Opens in new window) Facebook
    • Share on LinkedIn (Opens in new window) LinkedIn
    • Share on X (Opens in new window) X
    • Email a link to a friend (Opens in new window) Email

    Tags: Tags: 400guru, ASCII, EBCDIC, FHG, Four Hundred Guru, IBM i, RPG, SQL

    Sponsored by
    New Generation Software

    Put your IBM i Data to Work

    NGS-IQ makes it easy to securely access, format, analyze, and share critical business data across your organization.

    Join us for a look at the top challenges companies attending COMMON NAViGATE at TechXchange 2026 shared when it comes to getting reliable reports from their DB2 data — and how NGS-IQ can help provide the answers you need without breaking the budget.

    Register for the Webinar

    www.ngsi.com – (800)824-1220

    Share this:

    • Share on Reddit (Opens in new window) Reddit
    • Share on Facebook (Opens in new window) Facebook
    • Share on LinkedIn (Opens in new window) LinkedIn
    • Share on X (Opens in new window) X
    • Email a link to a friend (Opens in new window) Email

    The Price Tweaking Continues For Power Systems IBM To Add Full System Replication And FlashCopy To PowerHA

    Leave a ReplyCancel reply

TFH Volume: 35 Issue: 30

This Issue Sponsored By

  • Maxava
  • Computer Keyes
  • Manta Technologies
  • WorksRight Software
  • Raz-Lee Security

Table of Contents

  • Tool Aims To Streamline Git Integration For Old School IBM i Devs
  • IBM To Add Full System Replication And FlashCopy To PowerHA
  • Guru: Decoding Base64 ASCII
  • The Price Tweaking Continues For Power Systems
  • IBM i PTF Guide, Volume 27, Numbers 31 And 32

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • Power Systems Turns In Another Great Quarter
  • Big Blue Is Right On Schedule With Fall 2026 IBM i Tech Refreshes
  • Guru: Environment Variables – Global Variables Wearing Hiking Boots
  • A New IBM i Entity Takes Shape: Power By Fortra
  • IBM i PTF Guide, Volume 28, Number 38
  • Critical PHP2Shell Security Vulns Evoke ‘Black October’
  • Why Fortra Partnered with FalconStor to Launch Continuity Care: A Validated Recovery Service for IBM i
  • Guru: Analyzing User Session Statistics, Part 2
  • Fresche Launches OpsIQ, a Free AI-Based Operation Assistant for IBM i
  • IBM i PTF Guide, Volume 28, Number 37

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle