Manual Reference Pages  - base (3m_strings)

NAME

base(3f) - [M_strings:BASE] convert whole number string in base [2-36] to string in alternate base [2-36] (LICENSE:PD)

CONTENTS

Synopsis
Description
Options
Returns
Examples
Author
License

SYNOPSIS

elemental impure logical function base(x,b,y,a)

   character(len=*),intent(in)  :: x
   character(len=*),intent(out) :: y
   integer,intent(in)           :: b,a

DESCRIPTION

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.

OPTIONS

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

Returns .TRUE. if no error occurred, else returns .FALSE. .

EXAMPLES

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_base

Results:

   fpm: Entering directory ’/home/urbanjs/venus/V600/github/M_strings’
    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

AUTHOR

John S. Urban

LICENSE

Public Domain


Nemo Release 3.1 base (3m_strings) July 20, 2024
Generated by manServer 1.08 from 079b20f5-8daf-42d0-9850-3cd6a73d1916 using man macros.