Previous: Include Files Up: Porting Fortran Next: CHARACTER*n for Strings

No EQUIVALENCE for Type Conversion

The use of EQUIVALENCE to convert among data types, especially byte to integer, is widespread in current Fortran application code. A byte will typically be equivalenced to an integer, so storing the data in the byte variable makes it accessible via the integer variable. This practice is absolutely not portable. EQUIVALENCE should never be used to convert any data types. If you use EQUIVALENCE, it must be done to conserve storage only. You must access the data using the same data type you stored it with.

To do data type conversion, use the xvtrans family of routines. See Section , Converting Data Types & Hosts, for details. In addition, a method is available for converting between byte and integer (since it is by far the most common conversion) that is more efficient than calling the xvtrans routines. This method consists of the functions INT2BYTE and BYTE2INT. To use them, you must include the file ``fortport'' in the subroutine. ``fortport'' is a SUBLIB include, so it must be listed in FTNINC_LIST in the imakefile to be available. These conversion functions assume that the data is strictly in the 0 to 255 range. No bounds checking is performed. The functions are actually implemented on some machines as array lookups rather than functions, and on others they may be statement functions, so the include file must be present to get the appropriate definition.

        SUBROUTINE do_something(b, i)
        BYTE b
        INTEGER i
        INCLUDE 'fortport'
        b = INT2BYTE(i)
        i = BYTE2INT(b)