create procedure lib_math_base_to_int (
                     strg_in varchar(63),
                     base_in smallint)
                   returns (
                     val_out bigint)
  as
    declare variable strg_len smallint;
    declare variable strg_pos smallint;
    declare variable strg_chr varchar(1);
    declare variable val_add smallint;

    begin
      /*
        convert base-n (n=2..16) string to bigint (cardinal 0..2^63-1)
      */

      if (   (strg_in is null)
          or (strg_in = '')
          or (base_in is null)
          or (base_in < 2)
          or (16 < base_in))
        then
          val_out = null;  /* or: raise exception 'invalid parameter' */
        else
          begin
            val_out = 0;

            strg_in = upper( strg_in);
            execute procedure lib_string_len :strg_in returning_values :strg_len;
            strg_pos = 1;
            while (strg_pos <= strg_len) do
              begin
                execute procedure lib_string_sub :strg_in, :strg_pos, 1 returning_values :strg_chr;

                     if (   (strg_chr = '0')
                         or (strg_chr = '1')
                         or (strg_chr = '2')
                         or (strg_chr = '3')
                         or (strg_chr = '4')
                         or (strg_chr = '5')
                         or (strg_chr = '6')
                         or (strg_chr = '7')
                         or (strg_chr = '8')
                         or (strg_chr = '9')) then val_add = cast( strg_chr as smallint);
                else if (    strg_chr = 'A')  then val_add = 10;
                else if (    strg_chr = 'B')  then val_add = 11;
                else if (    strg_chr = 'C')  then val_add = 12;
                else if (    strg_chr = 'D')  then val_add = 13;
                else if (    strg_chr = 'E')  then val_add = 14;
                else if (    strg_chr = 'F')  then val_add = 15;
                else                               val_add = null;  /* or: raise exception 'invalid base-n character' */

                if (val_add < base_in)
                  then
                    val_out = (val_out * base_in) + val_add;
                  else
                    val_out = null;  /* or: raise exception 'invalid base-n character' */

                strg_pos = strg_pos + 1;
              end
          end

      suspend;
    end