User manual
mikroPascal PRO for dsPIC30/33 and PIC24
MikroElektronika
214
Explicit Conversion
Explicit conversion can be executed at any point by inserting type keyword (byte, word, short, integer,
longint or real) ahead of an expression to be converted. The expression must be enclosed in parentheses. Explicit
conversion can be performed only on the operand right of the assignment operator.
Special case is conversion between signed and unsigned types. Explicit conversion between signed and unsigned data
does not change binary representation of data — it merely allows copying of source to destination.
For example:
var a : byte; b : short;
...
b := -1;
a := byte(b); // a is 255, not 1
// This is because binary representation remains
// 11111111; it’s just interpreted differently now
You can’t execute explicit conversion on the operand left of the assignment operator:
word(b) := a; // Compiler will report an error
Conversions Examples
Here is an example of conversion:
program test;
type TBytePtr = ^byte;
var arr: array[10] of word;
ptr : TBytePtr;
var a, b, cc : byte;
dd : word;
begin
a := 241;
b := 128;
cc := a + b; // equals 113
cc := word(a + b); // equals 113
dd := a + b; // equals 369
ptr := TBytePtr(@arr);
ptr := ^byte(@arr);
end.