base(3f) - [M_strings:BASE] convert whole number string in base [2-36] to string in alternate base [2-36] (LICENSE:PD)
Synopsis
Description
Options
Returns
Examples
Author
License
elemental impure logical function base(x,b,y,a)
character(len=*),intent(in) :: x character(len=*),intent(out) :: y integer,intent(in) :: b,a
Convert a numeric string from base B to base A. The function returns FALSE if B is not in the range [2..36] or if string X contains invalid characters in base B or if result Y is too big.
The letters A,B,...,Z represent 10,11,...,36 in a base > 10.
x input string representing numeric whole value b assumed base of input string y output string. Y is assumed long enough to hold the computed value. If an error occurs Y is filled with asterisks (*). a base specified for output string
Returns .TRUE. if no error occurred, else returns .FALSE. .
Sample program:
program demo_base use M_strings, only: base implicit none integer :: ba, bd, i character(len=40) :: x, y character(len=*), parameter :: input(*) = [character(len=80) :: & 10 12345 10, & 2 10111 10, & 10 12345 20, & 10 abcdef 2, & 0 0 0] character(len=:),allocatable :: line print *, Base Conversion using base(3f) do i = 1, size(input) line=input(i) read (line, *) bd, x, ba if (x == 0) exit if (base(x, bd, y, ba)) then else print *, Error in decoding/encoding numbers end if write (*, (a," in base ",i0," is ",a," in base ",i0))& & trim(x),bd,trim(y),ba end do end program demo_baseResults:
> Base Conversion using base(3f) > 12345 in base 10 is 12345 in base 10 > 10111 in base 2 is 23 in base 10 > 12345 in base 10 is 1AH5 in base 20 > Error in decoding/encoding numbers > abcdef in base 10 is **************************************** in base 2
John S. Urban
Public Domain
Nemo Release 3.1 | base (3m_strings) | January 10, 2025 |