Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert Character To Numeric & Visa Versa

General

Convert Character To Numeric & Visa Versa

by  Skittle  Posted    (Edited  )
A handy little technique I noted down ages ago allows a conversion from a character field to a numeric field using an RPG IV BIF. Some sites have standard program or routines to do this but if you don't have one this might be handy.


The following code takes an alpha field value and converts it to a number before adding 10 to it just to prove it works. The one draw back to be wary of, is the occurance of alpha fields with all blanks in it. You must perform a condition before the %DEC BIF to make sure blanks are replaced with zeros or the %DEC will runtime fail.

*
*
DMyAlpha S 4A
DMyNumber S 4 0
*
*
*
/FREE
MyAlpha = '1234';

//
// Check Source For 0's Zeroes
// which in this case is not
// necessary as MyAlpha is
// hard coded to '1234'
//
If ( MyAlpha = *BLANKS );
MyAlpha = '0000';
Endif;


//
// Convert & Use
//
MyNumber = %DEC(MyAlpha : 4:0);
MyNumber = MyNumber + 10 ;

//
// Convert Back If You Like
//
MyAlpha = %CHAR(MyNumber);
/END-FREE

I think somebody told me how to do this on a forum ages ago but I still find it so handy I felt it deserved a FAQ.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top