Skip to main content

Type Casting

Explicit Casting

The most common use of value types is to explicitly cast from one type to another in expressions. To do this, you simply place the value type to cast to within parentheses. That creates a casting operator. Then place that casting operator immediately to the left of the expression to cast.

This converts the data from its original form to the new form (to keep the same bit-pattern, see the TRANSFER built-in function).

MyBoolean := (BOOLEAN) IF(SomeAttribute > 10,1,0);
          // casts the INTEGER values 1 and 0 to a BOOLEAN TRUE or FALSE
MyString := (STRING1) IF(SomeAttribute > 10,1,0);
          // casts the INTEGER values 1 and 0 to a 1-character string
          // containing '1' or '0'
MyValue := (INTEGER) MAP(MyString = '1' => MyString, '0');
          // casts the STRING values '1' and '0' to an INTEGER 1 or 0
MySet := (SET OF INTEGER1) [1,2,3,4,5,6,7,8,9,10];
          //casts from a SET OF INTEGER8 (the default) to SET OF INTEGER1

Implicit Casting

During expression evaluation, different value types may be implicitly cast in order to properly evaluate the expression. Implicit casting always means promoting one value type to another: INTEGER to STRING or INTEGER to REAL. BOOLEAN types may not be involved in mixed mode expressions. For example, when evaluating an expression using both INTEGER and REAL values, the INTEGER is promoted to REAL at the point where the two mix, and the result is a REAL value.

INTEGER and REAL may be freely mixed in expressions. At the point of contact between them the expression is treated as REAL. Until that point of contact the expression may be evaluated at INTEGER width. Division on INTEGER values implicitly promotes both operands to REAL before performing the division.

The following expression: (1+2+3+4)*(1.0*5)

evaluates as: (REAL)((INTEGER)1+(INTEGER)2+(INTEGER)3+(INTEGER)4)*(1.0*(REAL)5)

and: 5/2+4+5 evaluates as: (REAL)5/(REAL)2+(REAL)4+(REAL)5

while: '5' + 4 evaluates as: 5 + (STRING)4 //concatenation

Comparison operators are treated as any other mixed mode expression. Built-in Functions that take multiple values, any of which may be returned (such as MAP or IF), are treated as mixed mode expressions and will return the common base type. This common type must be reachable by standard implicit conversions.

Type Transfer

Type casting converts data from its original form to the new form. To keep the same bit-pattern you must use either the TRANSFER built-in function or the type transfer syntax, which is similar to type casting syntax with the addition of angle brackets (>valuetype<).

INTEGER1 MyInt := 65; //MyInt is an integer value 65
STRING1 MyVal := (>STRING1<) MyInt; //MyVal is "A" (ASCII 65)

Casting Rules

FromToResults in
INTEGERSTRINGASCII or EBCDIC representation of the value
DECIMALSTRINGASCII or EBCDIC representation of the value, including decimal and sign
REALSTRINGASCII or EBCDIC representation of the value, including decimal and sign--may be expressed in scientific notation
UNICODESTRINGASCII or EBCDIC representation with any non-existent characters appearing as the SUBstitute control code (0x1A in ASCII or 0x3F in EBCDIC) and any non-valid ASCII or EBCDIC characters appearing as the substitution codepoint (0xFFFD)
UTF8STRINGASCII or EBCDIC representation with any non-existent characters appearing as the SUBstitute control code (0x1A in ASCII or 0x3F in EBCDIC) and any non-valid ASCII or EBCDIC characters appearing as the substitution codepoint (0xFFFD)
STRINGQSTRINGUppercase ASCII representation
INTEGERUNICODEUNICODE representation of the value
DECIMALUNICODEUNICODE representation of the value, including decimal and sign
REALUNICODEUNICODE representation of the value, including decimal and sign--may be expressed in scientific notation
INTEGERUTF8UTF8 representation of the value
DECIMALUTF8UTF8 representation of the value, including decimal and sign
REALUTF8UTF8 representation of the value, including decimal and sign--may be expressed in scientific notation
INTEGERREALValue is cast with loss of precision when the value is greater than 15 significant digits
INTEGERREAL4Value is cast with loss of precision when the value is greater than 7 significant digits
STRINGREALSign, integer, and decimal portion of the string value
DECIMALREALValue is cast with loss of precision when the value is greater than 15 significant digits
DECIMALREAL4Value is cast with loss of precision when the value is greater than 7 significant digits
INTEGERDECIMALLoss of precision if the DECIMAL is too small
REALDECIMALLoss of precision if the DECIMAL is too small
STRINGDECIMALSign, integer, and decimal portion of the string value
STRINGINTEGERSign and integer portions of the string value
REALINTEGERInteger value, only--decimal portion is truncated
DECIMALINTEGERInteger value, only--decimal portion is truncated
INTEGERBOOLEAN0 = FALSE, anything else = TRUE
BOOLEANINTEGERFALSE = 0, TRUE = 1
STRINGBOOLEAN'' = FALSE, anything else = TRUE
BOOLEANSTRINGFALSE = '', TRUE = '1'
DATASTRINGValue is cast with no translation
STRINGDATAValue is cast with no translation
DATAUNICODEValue is cast with no translation
UNICODEDATAValue is cast with no translation
DATAUTF8Value is cast with no translation
UTF8DATAValue is cast with no translation
UTF8UNICODEValue is cast with no translation
UNICODEUTF8Value is cast with no translation

The casting rules for STRING to and from any numeric type apply equally to all string types, also. All casting rules apply equally to sets (using the SET OF type syntax).