Converting Primitive Types

You can convert any variable of a primitive type to another primitive type, by using the following syntax.

expression.primitive-type

The following example shows the conversion of a Date primitive type to a String primitive type.

writeDate(date: Date);
begin
    write "The date is " & date.String;
end;

As the changing of collection membership of primitive types is not supported by the reorganization process, a warning message is displayed when a collection membership of primitive types is changed from one primitive type to another and the class is already marked for reorganization or the change will cause the class to be marked for reorganization.

Converting any primitive value to a Binary primitive type obtains the internal representation of that value. Conversion of this intermediate Binary value to any type other than the original type may produce a different value from a direct conversion. For this reason, converting a primitive value from one type to another using an intermediate Binary value is not recommended. See also "Binary Primitive Type Conversions".

The following example shows an Integer primitive type being converted to a Real primitive type through an intermediate Binary value.

vars
    i : Integer;
begin
    i := 10;
    write i.Real;         //outputs 10
    write i.Binary.Real;  //outputs 4.94065645841247e-323    Not recommended
end;

The following table lists the effect of converting to the Byte and Character primitive types. Conversions from the MemoryAddress, Point, Date, Time, TimeStamp, TimeStampInterval, and TimeStampOffset types are not permitted.

From To Byte To Character
Binary The first byte is copied. In a Unicode system, the first two bytes of the Binary are taken as a Character (null‑padded, if necessary). In an ANSI system, the first byte of the Binary is taken as a Character.
     
Boolean

If true, the character is converted to 1; false is converted to zero (0).

If true, the character with ANSI or Unicode value equal to 1; if false, the Character with ANSI or Unicode value equal to zero (0).
     
Byte No change. Character with ANSI or Unicode value equal to the value of the Byte.
     
Character Numeric value in the range zero (0) through 255. If the value of the Character in a Unicode system is outside this range, an exception is raised. No change.
     
Decimal

Numeric value, truncated if necessary.

If the resulting Byte value is outside the range zero (0) through 255.9999, an exception is raised.

Character with ANSI or Unicode value equal to the integer part of the number.

If the resulting Character value is outside the range -255 through 255 ANSI, an exception is raised. For Unicode, the range that can be converted is zero (0) through 65535.9999, with no exception being raised.

     
Integer and Integer64 Numeric value (an exception is raised if the value exceeds the maximum size of the target).

Character with ANSI or Unicode value equal to the number.

If the resulting Character value is outside the range zero (0) through 255 ANSI or the range zero (0) through 65535 Unicode, an exception is raised.

     
Real

Numeric value, truncated if necessary.

If the resulting Byte value is outside the range zero (0) through 255, an exception is raised.

Character with ANSI or Unicode value equal to the integer part of the number.

If the resulting Character value is outside the range zero (0) through 255 ANSI or the range zero (0) through 65535 Unicode, an exception is raised.

     
String and StringUtf8

Numeric value represented by the string of digits. Only US‑ASCII characters in the range zero (0) through 9 are recognized.

The string can contain an optional string of tab and space characters, an optional sign, or a string of numeric values. The first unrecognized character ends the conversion. If the string cannot be converted to a numeric value, JADE returns zero (0), as shown in the following examples.

"abc".Byte = 0
"12abc".Byte = 12

Overflow exceptions occur if the result of the conversion is too large. Use the isByte method to check the contents of a string.

First character.

The following table lists the effect of converting to the Boolean primitive type. Conversion from the MemoryAddress and Point types is not permitted.

From To Boolean
Binary If all bits in the first byte are zero (0), then false; else true
Boolean No change
Byte If all bits are zero (0), then false; else true
Character If null, then false; else true
Date If null, then false; else true
Decimal If zero (0), then false; else true
Integer and Integer64 If zero (0), then false; else true
Real If zero (0), then false; else true
String and StringUtf8 If "true", then true; else false
Time If null, then false; else true
TimeStamp If null, then false; else true
TimeStampInterval If null, then false; else true
TimeStampOffset If null, then false; else true

The following table lists the effect of converting to the String and StringUtf8 primitive types.

From To String and StringUtf8
Binary A direct binary copy is made, which when converted to a StringUtf8, may result in an illegal byte sequence.
Boolean true is converted to "true"; false is converted to "false".
Byte Binary value of the Byte in a decimal format.
Character One-character string.
Date Date in the format dd MMMM yyyy. If the receiver has the invalid value, the string is "*invalid*". If the receiver is zero (0), an empty string is returned. The resulting string is always limited to ASCII digits and characters, it is never locale‑sensitive, and English month names are used.
Decimal Decimal number formatted to the required number of decimal places.
Integer and Integer64 Integer value.
MemoryAddress Memory address in a hexadecimal number format.
Point Cartesian coordinates in the format x,y.
Real Number displayed in expanded form.
String and StringUtf8 Typecasting assumes that the string to be converted contains ASCII characters only. Use the asString and asStringUtf8 methods for conversions based on a locale in an ANSI system.
Time Time in the format hh:mm:ss.
TimeStamp Date and time in the format dd MMMM yyyy, hh:mm:ss.
TimeStampInterval Timestamp interval in the format d:hh:mm:ss.fff.
TimeStampOffset Timestamp offset in the format d:hh:mm:ss.fff +|-hhmm.

The following table lists the effect of converting to the Integer and Integer64 primitive types. Conversions from the MemoryAddress, Point, TimeStamp, TimeStampInterval, and TimeStampOffset primitive types are not permitted.

From To Integer and Integer64
Binary First 4 or 8 bytes are copied as the Integer or Integer64 result, respectively. If the Binary value has fewer than 4 bytes or 8 bytes, the converted result is padded with null bytes.
Boolean true is converted to 1; false is converted to zero (0).
Byte Numeric value in the range zero (0) through 255.
Character Numeric value in the range zero (0) through 255 for an ANSI system, or zero (0) through 65,535 for a Unicode system.
Date Julian day number for Integer and Integer64.
Decimal Numeric value, truncated if necessary.
Integer and Integer64

Numeric value (an exception occurs if the value exceeds the maximum size of the target type).

Real

Numeric value (an exception occurs if the value exceeds the maximum size of the target type).

String and StringUtf8

Numeric value represented by the string of digits. Only US-ASCII characters in the range zero (0) through 9 are recognized.

The string can contain an optional string of tab and space characters, an optional sign, or a string of numeric values. The first unrecognized character ends the conversion. If the string cannot be converted to a numeric value, JADE returns zero (0), as shown in the following examples.

"abc".Integer = 0
"12abc".Integer = 12

Overflow exceptions occur if the result of the conversion is too large. Use the isInteger method or the isInteger64 method to check the contents of a string.

Time Milliseconds since midnight for Integer and Integer64.

The following table lists the effect of converting to the Decimal and Real primitive types. Conversions from MemoryAddress, Point, TimeStamp, TimeStampInterval, and TimeStampOffset types are not permitted.

From To Decimal and Real
Binary Sufficient bytes are copied to the Decimal or Real result (padded with null bytes, if necessary). The result of the conversion may not be meaningful.
Boolean true = 1, false = 0.
Byte Numeric value in the range zero (0) through 255.
Character Numeric value in the range zero (0) through 255 for an ANSI system, or zero (0) through 65,535 for a Unicode system.
Date Julian day number.
Decimal Numeric value.
Integer and Integer64 Numeric value.
Real Numeric value.
String and StringUtf8

Numeric value. Only US‑ASCII characters in the range 0 through 9 are recognized.

The string can contain an optional string of tab and space characters, an optional sign, or a string of numeric values. The first unrecognized character ends the conversion. If the string cannot be converted to a numeric value, JADE returns zero (0), as shown in the following examples.

"abc".Real = 0
"1.2abc".Decimal = 1.2

If you are uncertain of the contents of a string, call the isDecimal or the isReal method.

Time Milliseconds since midnight.

The following table lists the effect of converting to the Date and Time primitive types. Conversions from Boolean, Byte, Character, MemoryAddress, Point, TimeStampInterval, and TimeStampOffset types are not permitted.

From To Date To Time
Binary First 4 bytes are copied as the Date value. If the Binary value has fewer than 4 bytes, null bytes are used for padding. The result of conversion may not be meaningful. First 4 bytes are copied as the Time value. If the Binary value has fewer than 4 bytes, null bytes are used for padding. The result of conversion may not be meaningful.
Date No change. Not permitted.
Decimal Julian day number is converted to Date. Milliseconds since midnight is converted to Time.
Integer and Integer64 Julian day number is converted to Date. Milliseconds since midnight is converted to Time.
Real Julian day number is converted to Date. Milliseconds since midnight is converted to Time.
String and StringUtf8 Valid date string is converted to Date. Valid time string hh:mm[:ss.fff] [pre‑or‑post‑noon‑indicator] is converted to Time. (For details, see the rules that apply when casting a string to a time, following this table.)
Time Not permitted. No change.
TimeStamp Date part copied, Time part discarded. Time part copied, Date part discarded.

When casting a string to a date, the following rules apply.

When casting a string to a time, the following rules apply.

The following table lists the effect of converting to the TimeStamp and TimeStampInterval primitive types. Conversions from the Boolean, Byte, Character, Decimal, Integer, Integer64, MemoryAddress, Point, Real, and TimeStampOffset types are not permitted.

From To TimeStamp To TimeStampInterval
Binary First 8 bytes are copied as the TimeStamp value. If the Binary value has fewer than 8 bytes, null bytes are used for padding. Result of conversion may not be meaningful. First 8 bytes are copied as the TimeStampInterval value. If the Binary value has fewer than 8 bytes, null bytes are used for padding. Result of conversion may not be meaningful.
Date Date copied, Time null. Not permitted.
String and StringUtf8 Date and Time string converted to TimeStamp. In the format ddd:hh:mm:ss.fff.
Time Date null, Time copied. Not permitted.
TimeStamp No change. Not permitted.
TimeStampInterval Not permitted. No change.