Search code examples
stringcastingintegerfortran77

Fortran77 Casting integer to a String


Out of interest, I was trying to find a way to cast an integer to a String in Fortran77. I came across CHAR(I), but this converts the ASCII index I into the character in that postion. Is there a way to just simply cast an integer to a String in Fortran77? How about vice versa?


Solution

  • The Fortran way is to write the value of the integer into a string variable; this operation is known as an internal write. I'm heading out the door now so won't check this, and I have an ethical objection to writing FORTRAN77 or helping anyone else write it, so make no guarantee that the following doesn't contain bits of more modern Fortran.

    First declare a character variable to receive the integer

    character(len=12) :: int_as_string
    

    then write the integer into it as you would normally write an integer to any other channel such as stdout

    write(int_as_string,'(i12.12)') my_int
    

    I expect you'll want to set the format for writing the integer to something that suits you better