decodebase(3f) - [M_strings:BASE] convert whole number string in base [2-36] to base 10 number (LICENSE:PD)
Synopsis
Description
Options
Example
Author
License
logical function decodebase(string,basein,out10)
character(len=*),intent(in) :: string integer,intent(in) :: basein integer,intent(out) :: out10
Convert a numeric string representing a whole number in base BASEIN to base 10. The function returns FALSE if BASEIN is not in the range [2..36] or if string STRING contains invalid characters in base BASEIN or if result OUT10 is too big
The letters A,B,...,Z represent 10,11,...,36 in the base > 10.
string input string. It represents a whole number in the base specified by BASEIN unless BASEIN is set to zero. When BASEIN is zero STRING is assumed to be of the form BASE#VALUE where BASE represents the function normally provided by BASEIN. basein base of input string; either 0 or from 2 to 36. out10 output value in base 10
Sample program:
program demo_decodebase use M_strings, only : codebase, decodebase implicit none integer :: ba,bd character(len=40) :: x,y integer :: rprint *, BASE CONVERSION write(*,("Start Base (2 to 36): "),advance=no); read *, bd write(*,("Arrival Base (2 to 36): "),advance=no); read *, ba INFINITE: do print *, write(*,("Enter number in start base: "),advance=no); read *, x if(x.eq.0) exit INFINITE if(decodebase(x,bd,r)) then if(codebase(r,ba,y)) then write(*,("In base ",I2,": ",A20)) ba, y else print *,Error in coding number. endif else print *,Error in decoding number. endif enddo INFINITE
end program demo_decodebase
John S. Urban
Ref.: "Math matiques en Turbo-Pascal by M. Ducamp and A. Reverchon (2), Eyrolles, Paris, 1988".based on a F90 Version By J-P Moreau (www.jpmoreau.fr)
Public Domain
decodebase (3) | October 17, 2020 |