• 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
    WorksRight Software

    Do you need area code information?
    Do you need ZIP Code information?
    Do you need ZIP+4 information?
    Do you need city name information?
    Do you need county information?
    Do you need a nearest dealer locator system?

    We can HELP! We have affordable AS/400 software and data to do all of the above. Whether you need a simple city name retrieval system or a sophisticated CASS postal coding system, we have it for you!

    The ZIP/CITY system is based on 5-digit ZIP Codes. You can retrieve city names, state names, county names, area codes, time zones, latitude, longitude, and more just by knowing the ZIP Code. We supply information on all the latest area code changes. A nearest dealer locator function is also included. ZIP/CITY includes software, data, monthly updates, and unlimited support. The cost is $495 per year.

    PER/ZIP4 is a sophisticated CASS certified postal coding system for assigning ZIP Codes, ZIP+4, carrier route, and delivery point codes. PER/ZIP4 also provides county names and FIPS codes. PER/ZIP4 can be used interactively, in batch, and with callable programs. PER/ZIP4 includes software, data, monthly updates, and unlimited support. The cost is $3,900 for the first year, and $1,950 for renewal.

    Just call us and we’ll arrange for 30 days FREE use of either ZIP/CITY or PER/ZIP4.

    WorksRight Software, Inc.
    Phone: 601-856-8337
    Fax: 601-856-9432
    Email: software@worksright.com
    Website: www.worksright.com

    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

  • POWERUp 2025 –Your Source For IBM i 7.6 Information
  • Maxava Consulting Services Does More Than HA/DR Project Management – A Lot More
  • Guru: Creating An SQL Stored Procedure That Returns A Result Set
  • As I See It: At Any Cost
  • IBM i PTF Guide, Volume 27, Number 19
  • IBM Unveils Manzan, A New Open Source Event Monitor For IBM i
  • Say Goodbye To Downtime: Update Your Database Without Taking Your Business Offline
  • i-Rays Brings Observability To IBM i Performance Problems
  • Another Non-TR “Technology Refresh” Happens With IBM i TR6
  • IBM i PTF Guide, Volume 27, Number 18

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