• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Easy Conversion To ASCII

    September 29, 2015 Ted Holt

    The system we love so well has a long love affair with the EBCDIC collating sequence, but most of the world runs on ASCII and Unicode. Dealing with ASCII data has not been a trivial experience for RPG programmers, but IBM i 7.2 adds a feature that helps immensely.

    In ancient days, RPG programmers would call the QDCXLATE API, which uses a translation table, to convert EBCDIC to ASCII. This is probably not a good solution. IBM’s translation tables were good for their time, but times have changed.

    A modern solution is the iconv family of code conversion APIs, which use CCSIDs to convert data from one character set to another. These APIs do a great job, but to make them any more complicated we’d have to get the government involved.

    On a recent project I discovered a new way to convert data from one character set to another. As of 7.2, RPG allows you to specify the CCSID of alphanumeric (character) variables. This makes data conversion a snap. Here’s an example.

    dcl-s  DataToConvert    char(5);
    
    dcl-s  AsciiData      char(5)   ccsid(819);
    
    DataToConvert = '0 1 2'; 
    AsciiData = DataToConvert;
    

    Here are the values in hexadecimal.

    DataToConvert

    F0 40 F1 40
    F2

    ASCIIData

    30 20 31
    20 32

    Is that fine, or what?!

    But that’s not the way we’ve been using QDCXLATE and iconv all these years. We’ve been using those APIs to put ASCII data into EBCDIC variables. I hate that sort of thing because it’s the programming equivalent of a bald-faced lie. However, I have had to stuff ASCII into EBCDIC variables on more than one occasion in order to communicate properly with the outside world, and it will probably happen again.

    In that case, use a data structure, like this:

    dcl-s  DataToConvert    char(5);
    
    dcl-ds ConversionStructure;
       EBCDICData     char(5)               pos(1);
       ASCIIData      char(5)   ccsid(819)  pos(1);
    end-ds;                                        
    
    
    
    DataToConvert = '0 1 2';
    AsciiData = DataToConvert;
    // Do something with EBCDICData
    

    When this code runs, EBCDICData has the hexadecimal value 30 20 31 20 32, as one might expect.

    Here’s a fabulous technique that may reduce or eliminate the need to lie. If your RPG program needs to pass data to another program in ASCII format, specify the appropriate CCSID in the procedure prototype.

    dcl-pr  SomePgm   extpgm('SOMEPGM');
       Data      char(5)  ccsid(819)  const;
    end-pr;
    SomePgm (DataToConvert);
    

    The system courteously converts the data to the ASCII format that SOMEPGM requires.

    I’ve said good-bye to many old features of RPG and I don’t miss them. Now that I can specify the CCSID of character variables, I hope to say good-bye to QDCXLATE and iconv as well.

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    FalconStor

    Simplify Secure Offsite Data Protection for IBM Power with FalconStor Habanero™

    IBM i teams are under growing pressure to ensure data is protected, recoverable, and compliant—without adding complexity or disrupting stable environments.

    FalconStor Habanero™ provides secure, fully managed offsite data protection purpose-built for IBM Power. It integrates directly with existing IBM i backup tools and processes, enabling reliable offsite copies without new infrastructure, workflow changes, or added operational overhead.

    By delivering and managing the service end-to-end, FalconStor helps organizations strengthen cyber resilience, improve disaster recovery readiness, and meet compliance requirements with confidence. Offsite copies are securely maintained and available when needed, supporting recovery, audits, and business continuity.

    FalconStor Habanero offers a straightforward way to modernize offsite data protection for IBM i: focused on simplicity, reliability, and resilience.

    Learn More

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    BCD:  Download the IBM i ebook - The Business Case for IBM i Green Screen Modernization
    Storagepipe:  Offsite backup and disaster recovery for IBM i.
    System i Developer:  Session Grid Posted: RPG & DB2 Summit - Chicago, October 20-22

    Auditing Object Changes In A Production Environment More iSphere Goodies

    One thought on “Easy Conversion To ASCII”

    • David Nelson says:
      October 24, 2018 at 10:51 am

      Hello, can you do the same thing for ASCII to EBCDIC? If so what CCSID code would I use?

      I’m want to try this with my CGI RPG /FREE .

      dcl-s ebcdicBuff char(99500) ;
      dcl-s asciiBuff varchar(99500) ccsid(819) ;

      // Retrieve The JSON Document CONTENT_TYPE = application/json
      asciiBuff = GetStdInput() ;
      ebcdicBuff = asciiBuff ;
      ebcdicBuff = ClrWhiteSpace(wkBuff)

      Thank You
      David Nelson

      Reply

    Leave a Reply Cancel reply

Volume 15, Number 19 -- September 29, 2015
THIS ISSUE SPONSORED BY:

WorksRight Software
CCSS
LaserVault

Table of Contents

  • Auditing Object Changes In A Production Environment
  • Easy Conversion To ASCII
  • More iSphere Goodies

Content archive

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

Recent Posts

  • The Big Easy: Connecting The Dots On Big Blue’s AI Strategy For IBM i
  • From Migration To Maturity: The Cloud Reality For IBM i Shops
  • COMMON Dances To A Fresh New Tune In New Orleans
  • Eradani Touts Native Git Connection As AI Tools Spread
  • IBM i PTF Guide, Volume 28, Number 17
  • Power Systems Still Waiting For The GenAI Bump
  • The IBM i and the Hybrid Cloud World: Things To Keep In Mind
  • CData Adds Db2 for i Support to CDC Tool
  • As I See It: The Cost of Having Ethics
  • Brace Yourself: Another Power Systems Price Hike Coming May 1

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