Wednesday, April 27, 2016

Fortran 77 (F77) - How to Convert Integer to String and Concatenate

Recently was working with a 91-year old researcher on their Fortran 77 code written over the last 50-years. It needed debugging and some new coding so it lead to learning an ancient language lovingly called F77. It is an eye-opening experience to realize you are working on something with that much history and contributing to a legacy.

Anyhow, personal reflections aside, here is how you convert an integer into a string in fortran 77 and then concatenate it to another string. Useful for suffixing a filename for example.


The basic idea behind converting a variable of any type to a string in Fortran 77 is to write that variable into a Character array variable of some length using the Write function. So in our case we will be writing an Integer into a Character array.

Note: The size of the character array should match the number of digits else you will have a lot of white space. Of course you can do that if you want.

Let's define two such variables and assign them values.
        integer aNumber = 1024
        character*4 aString

        write(aString, 10) aNumber 
  10    format (I4) 

And that is it. You have done your conversion. It is a pair of lines is all.

Code Breakdown

It is kind of like explaining to a print function in C/C++ or other languages what the output type is supposed to be. If you don't know other languages that is fine. Keep reading.
        write(aString, 10) aNumber 
  10    format (I4)

The first line says: write the contents of the variable aNumber into the variable aString using the formatting provided on the line with the label 10 which will describe the format of the contents of the variables aNumber.

The second line says: This is the line with the label 10 which is where the write statement above refers to to get the formatting information of aNumber. In this case the format is I4 which means an Integer that is four digits long.

Larger Example Code

Here is a small sample code that will get the date and time and append the year to the end of a string. The concept being you can do this with filename strings to avoid output collisions as you likely do often when dealing with multiple iterations of a program run.

Note: The variable filename is set to be the exact length of the name to avoid the need for whitespace trimming. In practice, you would have to write some code to extract the actual substring and trim out the whitespace to make this work.
        integer timeArr(8)
        character*10 dateInfo(3)
        character*4 yearSuffix
        character*7 filename
        character*36 output

        filename = "my_file"

        call date_and_time(dateInfo(1), dateInfo(2), dateInfo(3),
     &  timeArr)

        write(yearSuffix, 10) timeArr(1)
  10    format (I4)
        output = filename // "-" // yearSuffix
        print *, output

Note: The details of the date_and_time function used below can be found on the Oracle documentation site.


Concatenation in Fortran is trivial. All it requires is using the "//" language syntax. So after you have done your conversion you can easily do something as follows.
        output = filename // "-" // yearSuffix
There are however a few caveats to consider.
  • If the filename variable is long (say 32 characters), but the name of the file itself is shorter than that length there will be trailing white spaces which will need trimming. Without that there will be a blank gap between filename and everything else on that line.
  • You need to ensure that the length of the final string here called output is equal to the sum of the other strings or greater. A smaller will obviously cut the overflow text off. In the above example, output needs to be at a minimum 11 characters long.

So there you have it. All done. Let me know if you have any questions, comments, and the like.