Re: [Beginner] advice needed

From: Paul Van Delst (paul.vandelst_at_noaa.gov)
Date: 10/28/04


Date: Thu, 28 Oct 2004 16:12:58 -0400

Jean-Marc wrote:
> Hello all,
>
> I'm new in Fortran programming, but not in programming.
> For training purposes, i wrote a simple function to
> convert an integer between 0 and 255 into the corresponding
> hexadecimal representation "00" to "FF".

[snip]

> Can you let me know how a .TRUE. Fortran programmer will code
> such a function (assuming that there is not an intrinsic function
> to do that). Once again, the purpose is not the conversion by
> iself, but the 'how to'.

I don't know if I should be considered a .TRUE. Fortran programmer (in the Boolean sense
at least :o), but this is what first occurred to me:

program blah
   integer :: i
   character(2) :: z
   write( *, * ) 'Enter a number [0-255]: '
   read(*,*) i
   select case (i)
     case (:0) ; z = '00'
     case (1:254) ; write( z, '( z2.2 )' ) i
     case (255:) ; z = 'FF'
   end select
   write(*,*)'Hex: ', z
end program blah

lnx:scratch : lf95 blah.f90
Encountered 0 errors, 0 warnings in file blah.f90.
lnx:scratch : a.out
  Enter a number [0-255]:
-34
  Hex: 00
lnx:scratch : a.out
  Enter a number [0-255]:
78
  Hex: 4E
lnx:scratch : a.out
  Enter a number [0-255]:
345
  Hex: FF

If you're stuck with f77, it's still doable, you'll just need to use IF.THEN.ELSE rather
than SELECT CASE.

cheers,

paulv


Quantcast