RPG – Conversion between alphanumeric and numeric

Convert Numeric to Alphanumeric with leading zeros using the RPG EDITC built in function.

w_alpha = %editc(icat:’X');

Convert alphanumeric to numeric using the DEC built in function.

// Example text:  Last payment 66.65         
repayment = %DEC(%SUBST(textField:14:16):15:2);

RPG – Extract multiple numeric values from text string

     H/TITLE FNDNUMCHR Find numerics in text string  
     h DEBUG DECEDIT(',') DATEDIT(*DMY/)   
     d textString      s            250    
     d validNumbers    c                   CONST('0123456789')  
     d zeros           c                   CONST('000000000000000000000')
     d listNumbers     s             20s 0 Dim(10)   

     d dsplyCounter    s              5s 0  
     d matchesFound    s              5s 0  
     d searchPosition  s              5s 0   
     d lengthOfNumber  s              5s 0    
     d endOfString     s              5s 0    
     d startOfNumber   s              5s 0  
     d endOfNumber     s              5s 0   
     d number          s             20s 0  
     d zeroPadded      s             21    
  /FREE     

   textString = '1 The balance of your account number 524949781'+
               ' is 2500'and the OD limit of EUR 750. Call ' +    
               ' 0356596991 for more information. We are open' +   
               ' from 8 to 8';  

   matchesFound = 0;     
   searchPosition = 1;   

   endOfString = %len(%trimr(textString));   

   DoW  searchPosition <= endOfString;  

      // find end of number-string by checking from left    
      // to right to find first non-numeric character.   
      endOfNumber = %check(validNumbers:textString:  
                                        searchPosition) - 1;  
     // find start of number-string by checking from right  
     // to left starting at the end of the number-string.  
     startOfNumber = %checkr(validNumbers:textString:endOfNumber) + 1;  

     // if a number has been found...     
     if startOfNumber <= endOfNumber; 
        // ...determine length...    
        lengthOfNumber = endOfNumber - startOfNumber + 1;     

        // ...convert variable-length number to fixed length  
        // padded with leading zeros...   
        zeroPadded = %subst(zeros:1:21-lengthOfNumber) +   
                     %subst(textString:startOfNumber:lengthOfNumber); 
        number = %dec(zeroPadded:21:0);   

        // ...and add to list of found numbers      
        matchesFound +=1 ;           
        listNumbers(matchesFound) = number;    
        searchPosition = endOfNumber + 2;  
     else;   
        searchPosition += 1;  
     endif;        

  EndDo;     

  // show the results on screen    
  dsplyCounter = 1;    
  DoW listNumbers(dsplyCounter) > 0;  
     Dsply  listNumbers(dsplyCounter);    
     dsplyCounter += 1;        
  EndDo;   

  Eval  *inlr = *on;     

 /end-free