 Type conversion: Char and integer

The programming language Chora supports different data types to represent integer operands. We distinguish between signed and unsigned integer data types (each with 8-, 16-, 32- or 64-bit precision). Additionally the data type char is available to represent 16-bit UNICODE character codes. In order to convert between the diverse integer and the char data types, a set of adequate character code to integer and one integer to character code conversion operators are available.

Syntax

Character code to integer conversion:

(int64)char‑operandorint64(char‑operand)

(int32)char‑operandorint32(char‑operand)

(int16)char‑operandorint16(char‑operand)

(int8)char‑operandorint8(char‑operand)

(uint64)char‑operandoruint64(char‑operand)

(uint32)char‑operandoruint32(char‑operand)

(uint16)char‑operandoruint16(char‑operand)

(uint8)char‑operandoruint8(char‑operand)

Integer to character code conversion:

(char)integer‑operandorchar(integer‑operand)

Discussion

The character code to integer conversion operator converts the given char operand to a signed or unsigned integer data type. Generally, the conversion to a compatible data type with the same or greater rank than the unsigned 16-bit character code do not change the operand value or its representation. Please note, the conversion to a higher rank extends the value in the operand by additional 0 (zero) bits:

var char  a1 = '7';  // dec 55,  hex 0x0037
var char  a2 = 'ñ';  // dec 241, hex 0x00F1
var char  a3 = 'Ω';  // dec 927, hex 0x039F

var int32 b1 = (int32)a1;  // b1 = 55  (hex 0x00000037)
var int32 b2 = (int32)a2;  // b2 = 241 (hex 0x000000F1)
var int32 b3 = (int32)a3;  // b3 = 927 (hex 0x0000039F)

Depending on the operand original content, the conversion may lead to the loss of information provided in it. Precisely, the conversion to a data type with lower rank results in the operand highest bits being truncated or reinterpreted. In simple terms, if the value stored originally in an operand doesn't fit in the destination data type, the value resulting from the conversion will differ from the expected value.

In the following example you see two char operands. While the value in the operand a1 is small enough to be converted in the lower int8 data type, the value in operand a2 doesn't fit in it and its conversion loses information resulting in a completely different value:

var char a1 = '7';  // dec 55,  hex 0x0037
var char a2 = 'ñ';  // dec 241, hex 0x00F1

var int8 b1 = (int8)a1;  // b1 =  55 (hex 0x37)
var int8 b2 = (int8)a2;  // b2 = -15 (hex 0xF1)

The integer to character code conversion operator converts the given signed or unsigned integer operand to a char data type. Generally, the conversion from a compatible data type with the same or lower rank than the unsigned 16-bit character code do not change the operand value or its representation. Please note, the conversion from a lower rank extends the value in the operand by additional 0 (zero) bits if the value was positive or 1 (one) bits if it was negative:

var int8   a1 = 55;   // hex 0x37
var uint8  a2 = 241;  // hex 0xF1
var uint16 a3 = 927;  // hex 0x039F
var int8   a4 = -15;  // hex 0xF1

var char b1 = (char)a1;  // b1 = '7'       dec 55,    hex 0x0037
var char b2 = (char)a2;  // b2 = 'ñ'       dec 241,   hex 0x00F1
var char b3 = (char)a3;  // b3 = 'Ω'       dec 927,   hex 0x039F
var char b4 = (char)a4;  // b4 = '\xFFF1'  dec 65521, hex 0xFFF1

Again, depending on the operand original content, the conversion may lead to the loss of information provided in it. Precisely, the conversion from a data type with rank higher than the unsigned 16-bit character code results in the operand highest bits being truncated or reinterpreted. In simple terms, if the value stored originally in an operand doesn't fit in the destination char data type, the value resulting from the conversion will differ from the expected value.

In the following example you see two int32 operands. While the value in the operand a1 is small enough to be converted in the lower char data type, the value in operand a2 doesn't fit in it and its conversion loses information resulting in a completely different value:

var int32 a1 = 55;    // hex 0x00000037
var int32 a2 = -1369; // hex 0xFFFFFAA7

var char b1 = (char)a1;  // b1 = '7'       dec 55,    hex 0x0037
var char b2 = (char)a2;  // b2 = '\xFAA7'  dec 64167, hex 0xFAA7

Explicit conversion

With the explicit conversion operator you specify explicitly how an operand should be treated within the implemented expression. Chora supports two equivalent notation variants how you can apply the conversion operators. In the first variant the destination data type is specified between a pair of (...) (parentheses). The second notation variant corresponds to a constructor invocation of the respective destination data type:

var int32 a = 30;
var int32 b = 5;

// Notation 1
var char c1 = (char)( a + b );

// Notation 2
var char c2 = char( a + b );

trace c1 == c2; // true

The explicit type conversion is performed with the above described side effects. Applying the conversion on an operand not fitting the destination data type will thus result in the original operand value being truncated or its sign being lost. Since you apply the operation consciously, the Chora compiler will not report any warning nor error in such case.

Implicit conversion

Wherever it is not ambiguous, the Chora compiler applies the necessary type conversion implicitly. This is usually the case when you assign the result of an expression to e.g. a variable or you pass it as parameter in a method invocation. In such cases the Chora compiler can derive from the destination operand the corresponding data type and decide whether or not to convert the expression. For example, the following assignment implies the conversion to char data type since the destination operand ch is a local variable declared with char as data type:

var int32 code = 87;
var char  ch;

// Apply an implicit conversion from 'int32' to 'char'
ch = code;  // ch = 'W'

If the applied implicit type conversion has potential side effects, the Chora compiler will report a warning giving you a chance to review the affected expression. To avoid the warning you can extend the expression by an additional explicit type conversion operator: 