base(3f) - [M_strings:BASE] convert whole number string in base [2-36]
to string in alternate base [2-36]
(LICENSE:PD)
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
a base specified for output string
Sample program:
program demo_base
use M_strings, only : base
implicit none
integer :: ba,bd
character(len=40) :: x,y
print *,' BASE CONVERSION'
write(*,'("Start Base (2 to 36): ")',advance='no'); read *, bd
write(*,'("Arrival Base (2 to 36): ")',advance='no'); read *, ba
INFINITE: do
write(*,'("Enter number in start base (0 to quit): ")',advance='no')
read *, x
if(x == '0') exit INFINITE
if(base(x,bd,y,ba))then
write(*,'("In base ",I2,": ",A20)') ba, y
else
print *,'Error in decoding/encoding number.'
endif
enddo INFINITE
end program demo_base
John S. Urban
Public Domain
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | x | |||
integer, | intent(in) | :: | b | |||
character(len=*), | intent(out) | :: | y | |||
integer, | intent(in) | :: | a |
logical function base(x,b,y,a)
character(len=*),intent(in) :: x
character(len=*),intent(out) :: y
integer,intent(in) :: b,a
integer :: temp
! ident_79="@(#) M_strings base(3f) convert whole number string in base [2-36] to string in alternate base [2-36]"
base=.true.
if(decodebase(x,b,temp)) then
if(codebase(temp,a,y)) then
else
print *,'Error in coding number.'
base=.false.
endif
else
print *,'Error in decoding number.'
base=.false.
endif
end function base