• 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:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

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

    Sponsored by
    Manta Technologies

    The Leader in IBM i Education!
    Need training on anything i?
    Manta is all you need.

    130 courses and competency exams on:
    · IBM i operations
    · System Management and Security
    · IBM i Programming Tools
    · Programming in RPG, COBOL, CL, Java
    · Web Development

    SQL, DB2, QueryProduct features:
    · Runs in every popular browser
    · Available 24/7/365
    · Free Student Reference Guides
    · Free Student Administration
    · Concurrent User License
    · Built-In IBM i Simulator

    You can download our 200-page catalog and take sample sessions at MantaTech.com

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

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

    Leave a Reply Cancel 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

  • 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
  • You Can Now Get IBM Tech Support For VS Code For i
  • Price Cut On Power S1012 Mini Since Power S1112 Ain’t Coming Until 2026
  • IBM i: Pro and Con
  • As I See It: Disruption
  • IBM i PTF Guide, Volume 27, Number 30

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