module mod_ascii
  implicit none
  private

  public :: ascii

  interface ascii
    module procedure x_ascii_chr
    module procedure x_ascii_str
  end interface ascii

  character(1), parameter, private :: dict_ascii(33:126) &
    = (/'!', '"', '#', '$', '%', '&', "'", '(', &  ! 21 ~ 28 ( 33 ~ 40)
        ')', '*', '+', ',', '-', '.', '/', '0', &  ! 29 ~ 30 ( 41 ~
        '1', '2', '3', '4', '5', '6', '7', '8', &  ! 31 ~ 38 ( 49 ~
        '9', ':', ';', '<', '=', '>', '?', '@', &  ! 39 ~ 40 ( 57 ~
        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', &  ! 41 ~ 48 ( 65 ~
        'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', &  ! 49 ~ 50 ( 73 ~
        'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', &  ! 51 ~ 58 ( 81 ~
        'Y', 'Z', '[', '\', ']', '^', '_', '`', &  ! 59 ~ 60 ( 89 ~
        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', &  ! 61 ~ 68 ( 97 ~
        'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', &  ! 69 ~ 70 (105 ~
        'q', 'r', 's', 't', 'u', 'v', 'w', 'x', &  ! 71 ~ 78 (113 ~
        'y', 'z', '{', '|', '}', '~' /)            ! 79 ~ 7d (121 ~ 126)


contains

function x_ascii_chr(x) result(ascii)
  implicit none
  integer(1), intent(in) :: x
  character(1)           :: ascii

  ascii = dict_ascii(x)
end function x_ascii_chr


function x_ascii_str(x) result(ascii)
  implicit none
  integer(1), intent(in) :: x(:)
  character(size(x))     :: ascii
  integer :: i

  do i = 1, size(x)
    ascii(i:i) = dict_ascii(x(i))
  enddo
end function x_ascii_str

end module mod_ascii