Iconv usage

From MidrangeWiki
Jump to: navigation, search

Use iconv() to convert CCSIDs

The following RPG fragment is an example of using the iconv() api to convert from the current job's CCSID to any other CCSID.

 /copy QSYSINC/QRPGLESRC,QTQICONV       

PconvertToCcsid...                                
P                 B                   export           
D                 PI         32767    varying          
D pInput                     32767    varying const    
D pToCCSID                       5  0 const            

D ibmIConvOpen    Pr            52a   extproc('QtqIconvOpen')  
D  fromcode                       *   value                    
D  tocode                         *   value                    
                                                               
D ibmIConv        Pr            10i 0 extproc('iconv')         
D  cd                           52a   value                    
D  inbuf                          *                            
D  inbytesleft                  10i 0                          
D  outbuf                         *                            
D  outbytesleft                 10i 0                          
                                                               
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 input           S          32767                                  
D output          S          32767                                  
                                                                    
Drc               S             10I 0                               

 /free                                                     
  fromcode.QTQCCSID = JOB_CCSID;                           
  tocode.QTQCCSID = pToCCSID;                              
  fromcode.QTQERVED02 = *allx'00';                         
  tocode.QTQERVED02 = *allx'00';                           
  iconv_t = ibmIconvOpen(%addr(tocode) : %addr(fromcode)); 
                                                          
  input = pInput;                                          
  inputPtr = %addr(input);                                 
  inputLen = %len(%trimr(input));                          
  outputPtr = %addr(output);                               
  outputLen = inputLen;                                    
                                                          
  rc = ibmIConv(iconv_t :                                  
     inputPtr  : inputLen:                                 
     outputPtr : outputLen);                               
                                            
  rc = ibmIconvClose( iconv_t );             
                                            
  return %TRIMR(output);                     
                                            
 /end-free                                   
P                 E               

This article is a stub. You can help by editing it.