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

    Migrate IBM i with Confidence

    Tired of costly and risky migrations? Maxava Migrate Live minimizes disruption with seamless transitions. Upgrading to Power10 or cloud hosted system, Maxava has you covered!

    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

    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

  • 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
  • Big Blue Raises IBM i License Transfer Fees, Other Prices
  • Keep The IBM i Youth Movement Going With More Training, Better Tools
  • Remain Begins Migrating DevOps Tools To VS Code
  • IBM Readies LTO-10 Tape Drives And Libraries
  • IBM i PTF Guide, Volume 27, Number 23

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