FORTRAN 77 Language Reference

DECODE/ENCODE

ENCODE writes to a character variable, array, or array element.@ DECODE reads from a character variable, array, or array element. Data is edited according to the format identifier.

Similar functionality can be accomplished, using internal files with formatted sequential WRITE statements and READ statements. ENCODE and DECODE are not in the FORTRAN 77 Standard, and are provided for compatibility with older versions of FORTRAN.

ENCODE (size, f, buf [, IOSTAT=ios] [, ERR=s]) [iolist]

DECODE (size, f, buf [, IOSTAT=ios] [, ERR=s]) [iolist]

Parameter 

Description 

size

Number of characters to be translated, an integer expression 

f

Format identifier, either the label of a FORMAT statement, or a character expression specifying the format string, or an asterisk.

buf

Variable, array, or array element 

ios

I/O status specifier, ios must be an integer variable or an integer array element.

s

The error specifier (statement label) s must be the label of executable statement in the same program unit in which the ENCODE and DECODE statement occurs.

iolist

List of input/output items.  

Description

The entities in the I/O list can be: variables, substrings, arrays, array elements, record fields. A simple unsubscripted array name specifies all of the elements of the array in memory storage order, with the leftmost subscript increasing more rapidly.

Execution proceeds as follows:

  1. The ENCODE statement translates the list items to character form according to the format identifier, and stores the characters in buf. A WRITE operation on internal files does the same.

  2. The DECODE statement translates the character data in buf to internal (binary) form according to the format identifier, and stores the items in the list. A READ statement does the same.

  3. If buf is an array, its elements are processed in the order of subscript progression, with the leftmost subscript increasing more rapidly.

  4. The number of characters that an ENCODE or a DECODE statement can process depends on the data type of buf. For example, an INTEGER*2 array can contain two characters per element, so that the maximum number of characters is twice the number of elements in that array. A character variable or character array element can contain characters equal in number to its length. A character array can contain characters equal in number to the length of each element multiplied by the number of elements.

  5. The interaction between the format identifier and the I/O list is the same as for a formatted I/O statement.

Example

A program using DECODE/ENCODE:


       CHARACTER S*6 / '987654' /, T*6 
       INTEGER V(3)*4 
       DECODE( 6, '(3I2)', S ) V 
       WRITE( *, '(3I3)') V 
       ENCODE( 6, '(3I2)', T ) V(3), V(2), V(1) 
       PRINT *, T 
       END

The above program has this output:


98 76 54 
547698

The DECODE reads the characters of S as 3 integers, and stores them into V(1), V(2), and V(3).

The ENCODE statement writes the values V(3), V(2), and V(1) into T as characters; T then contains '547698'.