• 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
    VISUAL LANSA 16 WEBINAR

    Trying to balance stability and agility in your IBM i environment?

    Join this webinar and explore Visual LANSA 16 – our enhanced professional low-code platform designed to help organizations running on IBM i evolve seamlessly for what’s next.

    🎙️VISUAL LANSA 16 WEBINAR

    Break Monolithic IBM i Applications and Unlock New Value

    Explore modernization without rewriting. Decouple monolithic applications and extend their value through integration with modern services, web frameworks, and cloud technologies.

    🗓️ July 10, 2025

    ⏰ 9 AM – 10 AM CDT (4 PM to 5 PM CEST)

    See the webinar schedule in your time zone

    Register to join the webinar now

    What to Expect

    • Get to know Visual LANSA 16, its core features, latest enhancements, and use cases
    • Understand how you can transition to a MACH-aligned architecture to enable faster innovation
    • Discover native REST APIs, WebView2 support, cloud-ready Azure licensing, and more to help transform and scale your IBM i applications

    Read more about V16 here.

    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

    IBM i, RPG, And Inaccurate Assumptions Keeping Firmware Up To Date

    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

  • Liam Allan Shares What’s Coming Next With Code For IBM i
  • From Stable To Scalable: Visual LANSA 16 Powers IBM i Growth – Launching July 8
  • VS Code Will Be The Heart Of The Modern IBM i Platform
  • The AS/400: A 37-Year-Old Dog That Loves To Learn New Tricks
  • IBM i PTF Guide, Volume 27, Number 25
  • Meet The Next Gen Of IBMers Helping To Build IBM i
  • Looks Like IBM Is Building A Linux-Like PASE For IBM i After All
  • Will Independent IBM i Clouds Survive PowerVS?
  • Now, IBM Is Jacking Up Hardware Maintenance Prices
  • IBM i PTF Guide, Volume 27, Number 24

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