Data conversion

This chapter discusses data conversions for computational data. PL/I converts data when a data item with a set of attributes is assigned to another data item with a different set of attributes.

Conversion of the value of a computational data item can change its internal representation, precision or mode (for arithmetic values), or length (for string values). The following tables summarize the circumstances that can cause conversion to other attributes.


Case Target attributes
Assignment Attributes of variable on left of assignment symbol
Operand in an expression Determined by rules for evaluation of expressions
Stream input (GET statement) Attributes of receiving field
Stream output (PUT statement) As determined by format list if stream is edit-directed, otherwise character-string
Argument to PROCEDURE or ENTRY Attributes of corresponding parameter
Argument to built-in function or pseudovariable Depends on the function or pseudovariable
INITIAL attribute Other attributes of variable being initialized
RETURN statement expression Attributes specified in PROCEDURE statement
DO statement, BY, TO, or REPEAT option Attributes of control variable

The following statements can cause conversion to a CHARACTER value.


Statement Option
DISPLAY  
Record I/O KEYFROMKEY
OPEN TITLE

The following statements can cause conversion to a BINARY value.


Statement Option/Attribute/Reference
DECLARE, ALLOCATE, DEFAULT length, size, dimension, bound, repetition factor
DELAY milliseconds
FORMAT (and format items in GET and PUT) iteration factor w, d, s, p
OPEN LINESIZE, PAGESIZE
I/O SKIP, LINE, IGNORE
Most statements subscript

All attributes for source and target data items (except string length) must be specified at compile time. Conversion can raise one of the following conditions: CONVERSION, OVERFLOW, SIZE, or STRINGSIZE. (See Conditions.)

Constants can be converted at compile time as well as at run time. In all cases, the conversions are as described in this topic collection.

In the discussions of conversions, note the meaning of M and N:

  • M is the maximum precision for FIXED BINARY. This is the value M2 from the compiler option LIMITS(FIXEDBIN(M1,M2)).
  • N is the maximum precision for FIXED DECIMAL. This is the value N2 from the compiler option LIMITS(FIXEDDEC(N1,N2)).

More than one conversion might be required for a particular operation. The implementation does not necessarily go through more than one. To understand the conversion rules, it is convenient to consider them separately. Consider the following example:

  dcl A fixed dec(3,2) init(1.23);
  dcl B fixed bin(15,5);
  B = A;

In this example, the decimal representation of 1.23 is first converted to a binary (11,7) value, as 1.0011101B. Then precision conversion is performed, resulting in a binary (15,5) value of 1.00111B.