Learn from sources
       Member XXXCVTSTG in CGIDEV2 / QRPGLESRC

      Hnomain
       /copy CGIDEV2/qrpglesrc,prototypeb
       /copy CGIDEV2/qrpglesrc,usec
       *================= Prototypes for calling ICONV API's ====================
       * Prototype for QtqIconvOpen (Code Conversion Allocation API)
       *The QtqIconvOpen() function performs the necessary initializations to convert character encod
       *ings from the source CCSID identified by the fromcode to the CCSID identified by the tocode.
       *It then returns a conversion descriptor of data type iconv_t. For EBCDIC mixed-byte encodings
       *, the conversion descriptor is set to the initial single-byte shift state.
      D QtqIconvOpen    pr                  extproc('QtqIconvOpen')
      D                                     like(iconv_t)
      D    toCode                           likeds(iconv_code) const
      D    fromCode                         likeds(iconv_code) const
       * Prototype for iconv (Code Conversion API)
       *The iconv() function converts a buffer of characters specified by the inbuf parameter from on
       *e coded character set identifier (CCSID) into another CCSID and stores the converted characte
       *rs into a buffer specified by the outbuf parameter. (The inbuf parameter points to a variable
       *that points to the first character in the input buffer. The outbuf parameter points to a vari
       *able that points to the first available byte in the output buffer.) The CCSIDs used are those
       *in the conversion descriptor, cd, which was returned from the call to either the iconv_open()
       *or the QtqIconvOpen() function.
      D*myiconv         pr            10i 0 extproc('iconv')
      D myiconv         pr                  extproc('iconv')
      D   cd                                like(iconv_t) value
      D   inbuf                         *
      D   inbytesleft                 10u 0
      D   outbuf                        *
      D   outbytesleft                10u 0
       *The iconv_close() function closes the conversion descriptor cd that was initialized by the
       *iconv_open() or QtqIconvOpen() function.
      D iconv_close     pr            10i 0 extproc('iconv_close')
      D   cd                                like(iconv_t) value
       *================= Prototypes for local subprocedures ====================
      DInitialiseConversion...
      D                 pr
      D  InpCodePage                  10u 0
      D  OutCodePage                  10u 0
       *================= Data structures for use of iconv ======================
       * Conversion descriptor (cd)
      D iconv_t         ds                  qualified
      D                                     based(StructureTemplate)
      D   return_value                10i 0                                      if error occurred
      D   cd                          10i 0 dim(12)
 
      D iconv_code      ds                  qualified
      D  ccsid                        10i 0 inz                                  ccsid
      D  ConvAlt                      10i 0 inz                                  cnv alternative
      D  SubsAlt                      10i 0 inz                                  subs alternative
      D  ShiftAlt                     10i 0 inz(1)                               shift alternative
      D  InpLenOp                     10i 0 inz(0)                               length option
      D  ErrorOpt                     10i 0 inz(1)                               mx error option
      D  Reserved                      8    inz(*allx'00')                       reserved
       *=============================================================================================
        // Conversion tables
      D gFromUNIC       ds                  likeds(iconv_t)
      D gToUNIC         ds                  likeds(iconv_t)
      D gConvFrom       ds                  likeds(iconv_code)
      D                                     inz(*LIKEDS)
      D gConvTo         ds                  likeds(iconv_code)
      D                                     inz(*LIKEDS)
       *=============================================================================================
      P CVTSTG          b                   export
      D CVTSTG          pi
      D  InpCodePage                  10u 0
      D  InpBufP                        *
      D  InpBufLen                    10u 0
      D  OutCodePage                  10u 0
      D  OutBufP                        *
      D  OutBufLen                    10u 0
      D  OutDtaLen                    10u 0
 
      D InpBufPSave     s               *
      D OutBufPSave     s               *
 
       /free
 
        // Save pointers, they may be modified by the iconv() API
        InpBufPSave=InpBufP;
        OutBufPSave=OutBufP;
 
        InitialiseConversion(InpCodePage:OutCodePage);
        outDtaLen=outBufLen;
        myiconv(gFromUNIC: InpBufP: InpBufLen: OutBufP: OutBufLen);
        outDtaLen=outDtaLen-OutBufLen;  //OutBufLen is now the length of non-translated data
 
        // Restore pointers
        InpBufP=InpBufPSave;
        OutBufP=OutBufPSave;
 
       /end-free
      P CVTSTG          e
       *=============================================================================================
      PInitialiseConversion...
      P                 b
      DInitialiseConversion...
      D                 pi
      D  InpCodePage                  10u 0
      D  OutCodePage                  10u 0
       /free
          gConvFrom.CCSID = InpCodePage;
          gConvTo.CCSID   = OutCodePage;
          gFromUNIC = QtqIConvOpen(gConvTo: gConvFrom);
       /end-free
      P                 e
0.062 sec.s