SQL Columns with CCSID 1200

I’ve recently been working on an ILE RPG program that needs to read a SQL table that has a VARCHAR column encoded with CCSID 1200 (Unicode).

Since RPG doesn’t normally handle Unicode, it takes a little extra effort to process the contents.

Here’s a quick example of how you handle this data …


The key to handling Unicode data in RPG is converting the data from CCSID 1200 to the job’s CCSID (which is specified as *ZERO). To do this, we need to use the iconv api.

The iconv api needs to be told what CCSID’s we are converting from & to.  In this case, the from CCSID is 1200.  The to CCSID is specified as *ZERO, which indicates that it should convert to the job’s current CCSID.

      // the QTQICONV copy source has the structures needed for iconv
      /copy QSYSINC/QRPGLESRC,QTQICONV

     D unicodefield    S            279
     D ebcdicfield     S            279

      // see http://midurl.com/qtqiconvopen for info on this api
     D ibmIConvOpen    Pr            52a   extproc('QtqIconvOpen')
     D  fromcode                       *   value
     D  tocode                         *   value

      // see http://midurl.com/iconv for info on this api
     D ibmIConv        Pr            10i 0 extproc('iconv')
     D  cd                           52a   value
     D  inbuf                          *
     D  inbytesleft                  10i 0
     D  outbuf                         *
     D  outbytesleft                 10i 0

      // see http://midurl.com/iconvclose for info on this api
     D ibmIconvClose   Pr            10i 0 extproc('iconv_close')
     D  cd                           52a   value

     D iconv_t         Ds
     D  t_rtnval                     10i 0
     D  t_cd
     D  t_cdi                        10i 0 Dim(12) Overlay(t_cd)

     D fromcode        DS                  likeds(QTQCODE) inz
     D tocode          DS                  likeds(QTQCODE) inz

     D inputPtr        S               *
     D inputLen        S             10I 0

     D outputPtr       S               *
     D outputLen       S             10I 0

     D rc              S             10I 0

      /free
       // read the unicode data into a program field
       exec sql
         select UNICODE_FIELD
           into :unicodeField
           from DATAFILE;

         // convert the unicode data to the job ccsid
         fromcode.QTQCCSID = 1200; // this is the FROM CCSID
         tocode.QTQCCSID = *zero; // this is the TO CCSID
         fromcode.QTQERVED02 = *allx'00';
         tocode.QTQERVED02 = *allx'00';
         iconv_t = ibmIconvOpen(%addr(tocode) : %addr(fromcode));

         inputPtr = %addr(unicodeField);
         inputLen = %len(%trimr(unicodeField));
         outputPtr = %addr(ebcdicfield);
         outputLen = inputLen;

         rc = ibmIConv(iconv_t :
           inputPtr  : inputLen:
           outputPtr : outputLen);

         rc = ibmIconvClose( iconv_t );

At this point in the program, the ebcdicfield contains the VARCHAR’s contents converted from Unicode into the current job’s CCSID.

Although I’m sure there are very valid technical reasons that DB2/400 can’t handle the conversion of Unicode data to the job’s CCSID (unlike other database CCSID conversions), it sure would be handy if it could.

About David:
David is a Sr. Software Engineer for MKS Software. He cut his teeth on the S/36 and has more than 20 years of experience on the IBM i / System i / iSeries / AS400. He primarily works in Java and ILE RPG specializing in cross platform integrations. David also runs and maintains midrange.com. His personal blog is Geeky Ramblings.

Popularity: 16%

Technorati Tags: , , ,

Tags: , , ,

2 Comments

  • Carey Evans says:

    Isn’t this what ILE RPG “C” type UCS-2 fields are for? They work well with F-specs, and seem to use iconv automatically under the covers.

  • David Gibbs says:

    Isn’t this what ILE RPG “C” type UCS-2 fields are for? They work well with F-specs, and seem to use iconv automatically under the covers.

    Actually, according to the Embedded SQL Programming reference, data type “C” is used for fields that are VARGRAPHIC or GRAPHIC with CCSID 13488.

    I tried using a “C” data type with the SQL program I wrote as a test and the SQL pre-compiler didn’t like it.

Leave a Reply

© 1993-2010 midrange.com - In My Humble Opinion All Rights Reserved