program base60 ! first step: Base60 encode. ! reference: http://faruk.akgul.org/blog/tantek-celiks-newbase60-in-python-and-java/ ! 5000 should be 1PL implicit none ! I have to declare the return type of the function in the main program, too. character(len=1000) :: numtosxg write(*,*) 0, trim(numtosxg(0)) write(*,*) 100000, trim(numtosxg(100000)) write(*,*) 1, trim(numtosxg(1)) write(*,*) 2, trim(numtosxg(2)) write(*,*) 60, trim(numtosxg(60)) write(*,*) 59, trim(numtosxg(59)) end program base60 character(len=1000) function numtosxg( number ) implicit none !!! preparation ! input: ensure that this is purely used as input. ! intent is only useful for function arguments. integer, intent(in) :: number ! constants: marked as parameter: not function parameters, but ! algorithm parameters! character(len=61), parameter :: base60chars = "0123456789"& //"ABCDEFGHJKLMNPQRSTUVWXYZ_abcdefghijkmnopqrstuvwxyz" ! work variables integer :: n = 0 integer :: remainder = 0 ! result character(len=1000) :: res = "" ! actual algorithm if (number == 0) then numtosxg = "0" return end if ! calculate the base60 string res = "" ! I have to explicitely set res to "", otherwise it ! accumulates the prior results! n = number ! the input argument: that should be safe to use. ! catch number = 0 do while(n > 0) remainder = mod(n, 60) n = n/60 ! note that fortran indizes start at 1, not at 0. res = base60chars(remainder+1:remainder+1)//trim(res) ! write(*,*) number, remainder, n end do numtosxg = res end function numtosxg