FORTRAN 77 Language Reference

### Octal and Hexadecimal Editing (O, Z)

The O and Z field descriptors for a FORMAT statement are for octal and hexadecimal integers, respectively, but they can be used with any data type.@

The general form is:

Ow[.m]
Zw[.m]

where w is the number of characters in the external field. For output, m, if specified, determines the total number of digits in the external field; that is, if there are fewer than m nonzero digits, the field is zero-filled on the left to a total of m digits. m has no effect on input.

#### Octal and Hex Input

A READ, with the O or Z field descriptors in the FORMAT, reads in w characters as octal or hexadecimal, respectively, and assigns the value to the corresponding member of the I/O list.

Example: Octal input, the external data field is:

`654321 `

The first digit in the example appears in input column 1.

The program that does the input is:

```	READ ( *, 2 ) M
2	FORMAT ( O6 ) ```

The above data and program result in the octal value 654321 being loaded into the variable M. Further examples are included in the following table.

Table 5-6 Sample Octal/Hex Input Values

Format

External Field

Internal (Octal or Hex) Value

O4

O4

O3

1234¤

16234

97¤¤¤

1234

1623

Error: "9" not allowed

Z5

Z5

Z4

A23DE¤

A23DEF

95.AF2

A23DE

A23DE

Error: "." not allowed

The general rules for octal and hex input are:

• For octal values, the external field can contain only numerals 0 through 7.

• For hexadecimal values, the external field can contain only numerals 0 through 9 and the letters A through F or a through f.

• Signs, decimal points, and exponent fields are not allowed.

• All-blank fields are treated as having a value of zero.

• If a data item is too big for the corresponding variable, an error message is displayed.

#### Octal and Hex Output

A WRITE, with the O or Z field descriptors in the FORMAT, writes out values as octal or hexadecimal integers, respectively. It writes to a field that is w characters wide, right-justified.

Example: Hex output:

```	M = 161
WRITE ( *, 8 ) M
8 	FORMAT ( Z3 )
END```

The program above displays A1 (161 decimal = A1 hex):

`¤A1 `

The letter A appears in output column 2.

Further examples are included in the following table.

Table 5-7 Sample Octal/Hex Output Value

Format

Internal (Decimal) Value

External (Octal/Hex) Representation

O6 O2 O4.3 O4.4 O632767 14251 27 27 -32767 D77777 ** D033 0033 *****
Z4 Z3.3 Z6.4 Z532767 2708 2708 -32767 7FFF A94 DD0A94 ******

The general rules for octal and hex output are:

• Negative values are written as if unsigned; no negative sign is printed.

• The external field is filled with leading spaces, as needed, up to the width w.

• If the field is too narrow, it is filled with asterisks.

• If m is specified, the field is left-filled with leading zeros, to a width of m.