# A.2 ENCODE and DECODE Statements

The ENCODE and DECODE statements translate data and transfer it between variables or arrays in internal storage. The ENCODE statement translates data from internal (binary) form to character form; the DECODE statement translates data from character to internal form. These statements are comparable to using internal files in formatted sequential WRITE and READ statements, respectively.

The ENCODE and DECODE statements take the following forms:

    ENCODE (c,f,b [,IOSTAT=ios] [,ERR=s]) [list]

    DECODE (c,f,b [,IOSTAT=ios] [,ERR=s]) [list]

c
  Is an integer expression. In the ENCODE statement, c is the number of characters (in bytes) to be translated to character form. In the DECODE statement, c is the number of characters to be translated to internal form.
f
  Is a format identifier. An error occurs if more than one record is specified.
b
  Is a scalar or array reference. If b is an array reference, its elements are processed in the order of subscript progression.

  In the ENCODE statement, b receives the characters after translation to external form. If less than c characters are received, the remaining character positions are filled with blank characters. In the DECODE statement, b contains the characters to be translated to internal form.

ios
  Is an integer scalar memory reference that is defined as a positive integer if an error occurs, and as zero if no error occurs.
s
  Is the label of an executable statement that receives control if an error occurs.
list
  Is an I/O list.

  In the ENCODE statement, the list contains the data to be translated to character form. In the DECODE statement, the list receives the data after translation to internal form.

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

Rules and Behavior

The number of characters that the ENCODE or DECODE statement can translate depends on the data type of b. 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.

If the variable or array is of character type, the maximum number of characters differs as follows:

- For character variables, the maximum number of characters is the length of the character variable.

- For character array elements, the maximum number of characters is the length of the character array element.

- For character arrays, the maximum number of characters is the length of each element multiplied by the number of elements.

Example

In the following example, the DECODE statement translates the 12 characters in A to integer form (as specified by FORMAT 100):

         DIMENSION K(3)

         CHARACTER*12 A,B

         DATA A/'123456789012'/

         DECODE (12,100,A) K

    100  FORMAT (3I4)

         ENCODE (12,100,B) K(3),  K(2),K(1)

The 12 characters are stored in array K:

    K(1)  = 1234

    K(2)  = 5678

    K(3)  = 9012

The ENCODE statement translates the values K(3), K(2), and K(1) to character form and stores the characters in the character variable B:

    B = '901256781234'

For More Information:

- On internal READ statements, see Section 7.2.4.

- On internal WRITE statements, see Section 7.3.4.

Previous Page Next Page Table of Contents