decodebase(3f) - [M_strings:BASE] convert whole number string in base [2-36] to base 10 number (LICENSE:PD)
Synopsis
Description
Options
Returns
Examples
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
Returns .true. if no error occurred, else returns .false. .
Sample program:
program demo_decodebase use M_strings, only : codebase, decodebase implicit none integer :: bd, i, r character(len=40) :: x character(len=*), parameter :: input(*) = [character(len=80) :: & 10 12345, & 2 10111, & 6 12345, & 10 abcdef, & 0 0] character(len=:),allocatable :: line print *, Base Conversion using decodebase(3f) do i = 1, size(input) line=input(i) read (line, *) bd, x if (x == 0) exit if(.not.decodebase(x,bd,r)) then print *,Error in decoding number. endif write (*, (a," in base ",i0," becomes ",i0," in base 10"))& & trim(x),bd,r end do end program demo_decodebaseResults:
> Base Conversion using decodebase(3f) > 12345 in base 10 becomes 12345 in base 10 > 10111 in base 2 becomes 23 in base 10 > 12345 in base 6 becomes 1865 in base 10 > Error in decoding number. > abcdef in base 10 becomes 0 in base 10
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
Nemo Release 3.1 | decodebase (3m_strings) | January 10, 2025 |