Difference between revisions of "Typecast"

From Free Pascal wiki
Jump to navigationJump to search
(content review, wikify HTML-table)
(→‎conversion versus typecasting: add Str procedure as a method of converting from enumerated type to a string)
 
(16 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{{Typecast}}
 
{{Typecast}}
Typecasting is the concept, allowing to [[Becomes|assign]] values of [[Variable|variables]] or literals, that do not match the variable's [[Data type|data type]], virtually overriding [[Pascal]]'s strong typing system.
+
Typecasting is the concept, allowing to [[Becomes|assign]] values of [[Variable|variables]] or expressions, that do not match the variable’s [[Data type|data type]], virtually overriding [[Pascal]]’s strong typing system.
 +
 
 +
== definition ==
 
Two flavors of typecasting are recognized:
 
Two flavors of typecasting are recognized:
; implicit typecasting
+
 
: If the complete range of values the source could have can be stored by the destination operand, an automatic – thus “implicit” – typecast occurs. For instance, all the values of a [[Byte|<syntaxhighlight lang="pascal" enclose="none">byte</syntaxhighlight>]] can be stored in an <syntaxhighlight lang="pascal" enclose="none">int64</syntaxhighlight>. Assigning a <syntaxhighlight lang="pascal" enclose="none">byte</syntaxhighlight>'s value to a <syntaxhighlight lang="pascal" enclose="none">int64</syntaxhighlight> works without problems, since the missing 0-bits are filled in automatically, i.e. implicitly.
+
=== implicit typecasting ===
; explict typecasting
+
If the complete range of values the source can be stored by the destination operand, an automatic – thus “implicit” – typecast occurs.
: If the source's range of value does not fit into the destination operand's range, [[FPC|the compiler]] will not compile the program, unless it is instructed to ignore this. There are two different explicit typecasts:
+
For instance, all the values of a [[Byte|<syntaxhighlight lang="pascal" inline>byte</syntaxhighlight>]] can be stored in an <syntaxhighlight lang="pascal" inline>int64</syntaxhighlight>.
:; value typecast
+
Assigning a <syntaxhighlight lang="pascal" inline>byte</syntaxhighlight>’s value to a <syntaxhighlight lang="pascal" inline>int64</syntaxhighlight> works without problems, since the missing 0-bits are filled in automatically, i.e. implicitly.
:: A value typecast is done, by prepending a data type identifier and surrounding the expression to typecast with parentheses, like this: <syntaxhighlight lang="pascal" enclose="none">dataType(expression)</syntaxhighlight>.
+
The programmer does not have to insert any additional code.
:; variable typecast
+
 
:: A variable typecast treats a variable as if it were a different type. Retrieving and storing the variable is done, as if it was the specified data type.
+
=== explicit typecasting ===
 +
If the source’s range of value does not fit into the destination operand’s range, [[FPC|the compiler]] will not compile the program, unless it is instructed to ignore this.
 +
There are two different explicit typecasts:
 +
 
 +
==== value typecast ====
 +
A value typecast is done, by prepending a data type identifier and surrounding the expression to typecast with parentheses, like this:
 +
<syntaxhighlight lang="pascal" inline>dataType(expression)</syntaxhighlight>.
 +
Extraneous bits are just cut off.
 +
This approach is usually used, if there is absolute certainty, the actual value of the <syntaxhighlight lang="pascal" inline>expression</syntaxhighlight> will fit into the destination.
 +
Occasionally this effect is also used instead of a [[Mod|modulo operation]].
 +
 
 +
==== variable typecast ====
 +
A variable typecast treats a variable as if it were a different type.
 +
Retrieving and storing the variable is done, as if it was the specified data type.
 +
Just as a value typecast, the data type identifier is prepended and parentheses surround, in this case, a variable identifier:
 +
<syntaxhighlight lang="pascal" inline>dataTypeIdentifier(variableIdentifier)</syntaxhighlight>.
 +
Unlike a value typecast, the variable typecast can occur on both sides of an assignment.
  
 
== conversion versus typecasting ==
 
== conversion versus typecasting ==
 +
Type conversion is the ordered process of mapping the domain’s values to a co-domain, possibly triggering [[Exceptions|exceptions]] or [[runtime error|run-time errors]].
 +
This is done by properly defined [[Function|functions]].
 +
Bare typecasts on the other hand are always with brute force.
 +
They cut and push the bits 1:1 to the destination.
 +
However, you may define an [[Operator overloading|operator overload]] redefining this behavior.
 +
 
In some instances, you can convert values:
 
In some instances, you can convert values:
 
{| class="wikitable" style="text-align: center"
 
{| class="wikitable" style="text-align: center"
|+ conversion
+
|+ conversion opportunities
 
! soure data type !! target data type !! type of type conversion !! method
 
! soure data type !! target data type !! type of type conversion !! method
 
|-
 
|-
| [[Integer|<syntaxhighlight lang="pascal" enclose="none">integer</syntaxhighlight>]]
+
| [[Integer|<syntaxhighlight lang="pascal" inline>integer</syntaxhighlight>]]
| [[Real|<syntaxhighlight lang="pascal" enclose="none">real</syntaxhighlight>]]
+
| [[Real|<syntaxhighlight lang="pascal" inline>real</syntaxhighlight>]]
 
| implicit
 
| implicit
 
| assignment statement
 
| assignment statement
 
|-
 
|-
| <syntaxhighlight lang="pascal" enclose="none">real</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>real</syntaxhighlight>
| <syntaxhighlight lang="pascal" enclose="none">integer</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>integer</syntaxhighlight>
 
| explicit
 
| explicit
* cut off fractional part
 
* round fractional part
 
 
|
 
|
* [[Trunc|<syntaxhighlight lang="pascal" enclose="none">trunc</syntaxhighlight>]]
+
* [[Trunc|<syntaxhighlight lang="pascal" inline>trunc</syntaxhighlight>]] cuts off fractional part
* [[Round|<syntaxhighlight lang="pascal" enclose="none">round</syntaxhighlight>]]
+
* [[Round|<syntaxhighlight lang="pascal" inline>round</syntaxhighlight>]] rounds fractional part
 
|-
 
|-
| <syntaxhighlight lang="pascal" enclose="none">integer</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>integer</syntaxhighlight>
| [[String|<syntaxhighlight lang="pascal" enclose="none">string</syntaxhighlight>]]
+
| [[String|<syntaxhighlight lang="pascal" inline>string</syntaxhighlight>]]
 
| explicit
 
| explicit
| {{Doc|package=RTL|unit=sysutils|identifier=inttostr|text=<syntaxhighlight lang="pascal" enclose="none">sysUtils.intToStr</syntaxhighlight>}}
+
| {{Doc|package=RTL|unit=sysutils|identifier=inttostr|text=<syntaxhighlight lang="pascal" inline>sysUtils.intToStr</syntaxhighlight>}}
 
|-
 
|-
| <syntaxhighlight lang="pascal" enclose="none">real</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>real</syntaxhighlight>
| <syntaxhighlight lang="pascal" enclose="none">string</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>string</syntaxhighlight>
 
| explicit
 
| explicit
 
|
 
|
* {{Doc|package=RTL|unit=sysutils|identifier=floattostr|text=<syntaxhighlight lang="pascal" enclose="none">sysUtils.floatToStr</syntaxhighlight>}}
+
* {{Doc|package=RTL|unit=sysutils|identifier=floattostr|text=<syntaxhighlight lang="pascal" inline>sysUtils.floatToStr</syntaxhighlight>}}
* {{Doc|package=RTL|unit=sysutils|identifier=floattostrf|text=<syntaxhighlight lang="pascal" enclose="none">sysUtils.floatToStrF</syntaxhighlight>}}
+
* {{Doc|package=RTL|unit=sysutils|identifier=floattostrf|text=<syntaxhighlight lang="pascal" inline>sysUtils.floatToStrF</syntaxhighlight>}}
 
|-
 
|-
| <syntaxhighlight lang="pascal" enclose="none">string</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>string</syntaxhighlight>
| <syntaxhighlight lang="pascal" enclose="none">integer</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>integer</syntaxhighlight>
 
| explicit
 
| explicit
| {{Doc|package=RTL|unit=sysutils|identifier=strtoint|text=<syntaxhighlight lang="pascal" enclose="none">sysUtils.strToInt</syntaxhighlight>}}
+
| {{Doc|package=RTL|unit=sysutils|identifier=strtoint|text=<syntaxhighlight lang="pascal" inline>sysUtils.strToInt</syntaxhighlight>}}
 
|-
 
|-
| <syntaxhighlight lang="pascal" enclose="none">string</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>string</syntaxhighlight>
| <syntaxhighlight lang="pascal" enclose="none">real</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>real</syntaxhighlight>
 
| explicit
 
| explicit
| {{Doc|package=RTL|unit=sysutils|identifier=strtofloat|text=<syntaxhighlight lang="pascal" enclose="none">sysUtils.strToFloat</syntaxhighlight>}}
+
| {{Doc|package=RTL|unit=sysutils|identifier=strtofloat|text=<syntaxhighlight lang="pascal" inline>sysUtils.strToFloat</syntaxhighlight>}}
 
|-
 
|-
| <syntaxhighlight lang="pascal" enclose="none">string</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>string</syntaxhighlight>
| [[Char|<syntaxhighlight lang="pascal" enclose="none">char</syntaxhighlight>]]
+
| [[Char|<syntaxhighlight lang="pascal" inline>char</syntaxhighlight>]]
 
| explicit
 
| explicit
| <syntaxhighlight lang="pascal" enclose="none">stringVariable[indexExpression]</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>stringVariable[indexExpression]</syntaxhighlight>
 
|-
 
|-
| <syntaxhighlight lang="pascal" enclose="none">char</syntaxhighlight>/<syntaxhighlight lang="pascal" enclose="none">ANSIChar</syntaxhighlight>/<syntaxhighlight lang="pascal" enclose="none">wideChar</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>char</syntaxhighlight>/<syntaxhighlight lang="pascal" inline>ANSIChar</syntaxhighlight>/<syntaxhighlight lang="pascal" inline>wideChar</syntaxhighlight>
| <syntaxhighlight lang="pascal" enclose="none">string</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>string</syntaxhighlight>
 
| implicit
 
| implicit
 
| assignment statement
 
| assignment statement
 
|-
 
|-
| <syntaxhighlight lang="pascal" enclose="none">char</syntaxhighlight>/<syntaxhighlight lang="pascal" enclose="none">ANSIChar</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>char</syntaxhighlight>/<syntaxhighlight lang="pascal" inline>ANSIChar</syntaxhighlight>
| <syntaxhighlight lang="pascal" enclose="none">byte</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>byte</syntaxhighlight>
 
| explicit
 
| explicit
 
|
 
|
* [[Ord|<syntaxhighlight lang="pascal" enclose="none">ord</syntaxhighlight>]]
+
* [[Ord|<syntaxhighlight lang="pascal" inline>ord</syntaxhighlight>]]
* <syntaxhighlight lang="pascal" enclose="none">byte(characterVariableOrExpression)</syntaxhighlight>
+
* <syntaxhighlight lang="pascal" inline>byte(characterVariableOrExpression)</syntaxhighlight>
 
|-
 
|-
| <syntaxhighlight lang="pascal" enclose="none">byte</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>byte</syntaxhighlight>
| <syntaxhighlight lang="pascal" enclose="none">char</syntaxhighlight>/<syntaxhighlight lang="pascal" enclose="none">ANSIChar</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>char</syntaxhighlight>/<syntaxhighlight lang="pascal" inline>ANSIChar</syntaxhighlight>
 
| explicit
 
| explicit
 
|
 
|
* [[Chr|<syntaxhighlight lang="pascal" enclose="none">chr</syntaxhighlight>]]
+
* [[Chr|<syntaxhighlight lang="pascal" inline>chr</syntaxhighlight>]]
* <syntaxhighlight lang="pascal" enclose="none">ANSIChar(byteVariableOrExpression)</syntaxhighlight>
+
* <syntaxhighlight lang="pascal" inline>ANSIChar(byteVariableOrExpression)</syntaxhighlight>
 
|-
 
|-
 
| enumerated type
 
| enumerated type
| <syntaxhighlight lang="pascal" enclose="none">string</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>string</syntaxhighlight>
 
| explicit
 
| explicit
| {{Doc|package=RTL|unit=system|identifier=writestr|text=<syntaxhighlight lang="pascal" enclose="none">system.writeStr</syntaxhighlight>}}<syntaxhighlight lang="pascal" enclose="none">(stringVariable, enumeratedVariableOrExpression)</syntaxhighlight>
+
|
 +
* {{Doc|package=RTL|unit=system|identifier=str|text=<syntaxhighlight lang="pascal" inline>system.Str</syntaxhighlight>}}<syntaxhighlight lang="pascal" inline>(enumeratedVariableOrExpression, stringVariable)</syntaxhighlight>
 +
* {{Doc|package=RTL|unit=system|identifier=writestr|text=<syntaxhighlight lang="pascal" inline>system.writeStr</syntaxhighlight>}}<syntaxhighlight lang="pascal" inline>(stringVariable, enumeratedVariableOrExpression)</syntaxhighlight>
 
|}
 
|}
 
In other cases you manually have to perform explicit typecasts:
 
In other cases you manually have to perform explicit typecasts:
Line 87: Line 111:
 
! source data type !! target data type !! type of type conversion !! method
 
! source data type !! target data type !! type of type conversion !! method
 
|-
 
|-
| [[QWord|<syntaxhighlight lang="pascal" enclose="none">qWord</syntaxhighlight>]]
+
| [[QWord|<syntaxhighlight lang="pascal" inline>qWord</syntaxhighlight>]]
| <syntaxhighlight lang="pascal" enclose="none">byte</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>byte</syntaxhighlight>
 
| explicit
 
| explicit
| <syntaxhighlight lang="pascal" enclose="none">byte(qWordVariableOrExpression)</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>byte(qWordVariableOrExpression)</syntaxhighlight>
 
|-
 
|-
| <syntaxhighlight lang="pascal" enclose="none">qWord</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>qWord</syntaxhighlight>
| <syntaxhighlight lang="pascal" enclose="none">word</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>word</syntaxhighlight>
 
| explicit
 
| explicit
| <syntaxhighlight lang="pascal" enclose="none">word(qWordVariableOrExpression)</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>word(qWordVariableOrExpression)</syntaxhighlight>
 
|-
 
|-
| <syntaxhighlight lang="pascal" enclose="none">qWord</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>qWord</syntaxhighlight>
| [[Cardinal|<syntaxhighlight lang="pascal" enclose="none">cardinal</syntaxhighlight>]]
+
| [[Cardinal|<syntaxhighlight lang="pascal" inline>cardinal</syntaxhighlight>]]
 
| explicit
 
| explicit
| <syntaxhighlight lang="pascal" enclose="none">cardinal(qWordVariableOrExpression)</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>cardinal(qWordVariableOrExpression)</syntaxhighlight>
 
|-
 
|-
| <syntaxhighlight lang="pascal" enclose="none">qWord</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>qWord</syntaxhighlight>
| <syntaxhighlight lang="pascal" enclose="none">longWord</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>longWord</syntaxhighlight>
 
| explicit
 
| explicit
| <syntaxhighlight lang="pascal" enclose="none">longWord(qWordVariableOrExpression)</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>longWord(qWordVariableOrExpression)</syntaxhighlight>
 
|-
 
|-
| <syntaxhighlight lang="pascal" enclose="none">longWord</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>longWord</syntaxhighlight>
| <syntaxhighlight lang="pascal" enclose="none">byte</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>byte</syntaxhighlight>
 
| explicit
 
| explicit
| <syntaxhighlight lang="pascal" enclose="none">byte(longWordVariableOrExpression)</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>byte(longWordVariableOrExpression)</syntaxhighlight>
 
|-
 
|-
| <syntaxhighlight lang="pascal" enclose="none">longWord</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>longWord</syntaxhighlight>
| <syntaxhighlight lang="pascal" enclose="none">word</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>word</syntaxhighlight>
 
| explicit
 
| explicit
| <syntaxhighlight lang="pascal" enclose="none">word(longWordVariableOrExpression)</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>word(longWordVariableOrExpression)</syntaxhighlight>
 
|-
 
|-
| <syntaxhighlight lang="pascal" enclose="none">longWord</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>longWord</syntaxhighlight>
| <syntaxhighlight lang="pascal" enclose="none">cardinal</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>cardinal</syntaxhighlight>
 
| implicit
 
| implicit
 
| assignment statement
 
| assignment statement
 
|-
 
|-
| <syntaxhighlight lang="pascal" enclose="none">int64</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>int64</syntaxhighlight>
| <syntaxhighlight lang="pascal" enclose="none">byte</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>byte</syntaxhighlight>
 
| explicit
 
| explicit
| <syntaxhighlight lang="pascal" enclose="none">byte(int64variableOrExpression)</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>byte(int64variableOrExpression)</syntaxhighlight>
 
|-
 
|-
| <syntaxhighlight lang="pascal" enclose="none">int64</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>int64</syntaxhighlight>
| <syntaxhighlight lang="pascal" enclose="none">shortInt</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>shortInt</syntaxhighlight>
 
| explicit
 
| explicit
| <syntaxhighlight lang="pascal" enclose="none">shortInt(int64variableOrExpression)</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>shortInt(int64variableOrExpression)</syntaxhighlight>
 
|-
 
|-
| [[Comp|<syntaxhighlight lang="pascal" enclose="none">comp</syntaxhighlight>]]
+
| [[Comp|<syntaxhighlight lang="pascal" inline>comp</syntaxhighlight>]]
| <syntaxhighlight lang="pascal" enclose="none">byte</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>byte</syntaxhighlight>
 
| explicit
 
| explicit
| <syntaxhighlight lang="pascal" enclose="none">byte(compVariableOrExpression)</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>byte(compVariableOrExpression)</syntaxhighlight>
 
|-
 
|-
| <syntaxhighlight lang="pascal" enclose="none">comp</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>comp</syntaxhighlight>
| <syntaxhighlight lang="pascal" enclose="none">shortInt</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>shortInt</syntaxhighlight>
 
| explicit
 
| explicit
| <syntaxhighlight lang="pascal" enclose="none">shortInt(compVariableOrExpression)</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>shortInt(compVariableOrExpression)</syntaxhighlight>
 
|-
 
|-
| <syntaxhighlight lang="pascal" enclose="none">comp</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>comp</syntaxhighlight>
| <syntaxhighlight lang="pascal" enclose="none">real</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>real</syntaxhighlight>
 
| explicit
 
| explicit
| <syntaxhighlight lang="pascal" enclose="none">real(compVariableOrExpression)</syntaxhighlight>
+
| <syntaxhighlight lang="pascal" inline>real(compVariableOrExpression)</syntaxhighlight>
 
|}
 
|}
 +
 +
== caveats ==
 +
* Explicit typecasts disable range checks altogether in the complete [[LOC|line of code]].
 +
 +
== see also ==
 +
* [https://en.wikipedia.org/wiki/Type_casting_(computer_programming) “type casting (computer programming)”] in the English Wikipedia
 +
* [https://www.freepascal.org/docs-html/ref/refse84.html § “value typecasts”] in the FreePascal Reference guide
 +
* [https://www.freepascal.org/docs-html/ref/refse85.html § “variable typecasts”] in the FreePascal Reference guide
 +
* [https://www.freepascal.org/docs-html/ref/refse86.html § “<syntaxhighlight lang="pascal" inline>unaligned</syntaxhighlight> typecasts”] in the FreePascal Reference guide
 +
* [[Operator overloading|operator overloading]] (especially the group of assignment operators, and § “routing”)

Latest revision as of 02:44, 18 October 2023

Deutsch (de) English (en) français (fr) русский (ru)
Typecasting is the concept, allowing to assign values of variables or expressions, that do not match the variable’s data type, virtually overriding Pascal’s strong typing system.

definition

Two flavors of typecasting are recognized:

implicit typecasting

If the complete range of values the source can be stored by the destination operand, an automatic – thus “implicit” – typecast occurs. For instance, all the values of a byte can be stored in an int64. Assigning a byte’s value to a int64 works without problems, since the missing 0-bits are filled in automatically, i.e. implicitly. The programmer does not have to insert any additional code.

explicit typecasting

If the source’s range of value does not fit into the destination operand’s range, the compiler will not compile the program, unless it is instructed to ignore this. There are two different explicit typecasts:

value typecast

A value typecast is done, by prepending a data type identifier and surrounding the expression to typecast with parentheses, like this: dataType(expression). Extraneous bits are just cut off. This approach is usually used, if there is absolute certainty, the actual value of the expression will fit into the destination. Occasionally this effect is also used instead of a modulo operation.

variable typecast

A variable typecast treats a variable as if it were a different type. Retrieving and storing the variable is done, as if it was the specified data type. Just as a value typecast, the data type identifier is prepended and parentheses surround, in this case, a variable identifier: dataTypeIdentifier(variableIdentifier). Unlike a value typecast, the variable typecast can occur on both sides of an assignment.

conversion versus typecasting

Type conversion is the ordered process of mapping the domain’s values to a co-domain, possibly triggering exceptions or run-time errors. This is done by properly defined functions. Bare typecasts on the other hand are always with brute force. They cut and push the bits 1:1 to the destination. However, you may define an operator overload redefining this behavior.

In some instances, you can convert values:

conversion opportunities
soure data type target data type type of type conversion method
integer real implicit assignment statement
real integer explicit
  • trunc cuts off fractional part
  • round rounds fractional part
integer string explicit sysUtils.intToStr
real string explicit
string integer explicit sysUtils.strToInt
string real explicit sysUtils.strToFloat
string char explicit stringVariable[indexExpression]
char/ANSIChar/wideChar string implicit assignment statement
char/ANSIChar byte explicit
  • ord
  • byte(characterVariableOrExpression)
byte char/ANSIChar explicit
  • chr
  • ANSIChar(byteVariableOrExpression)
enumerated type string explicit

In other cases you manually have to perform explicit typecasts:

typecasting
source data type target data type type of type conversion method
qWord byte explicit byte(qWordVariableOrExpression)
qWord word explicit word(qWordVariableOrExpression)
qWord cardinal explicit cardinal(qWordVariableOrExpression)
qWord longWord explicit longWord(qWordVariableOrExpression)
longWord byte explicit byte(longWordVariableOrExpression)
longWord word explicit word(longWordVariableOrExpression)
longWord cardinal implicit assignment statement
int64 byte explicit byte(int64variableOrExpression)
int64 shortInt explicit shortInt(int64variableOrExpression)
comp byte explicit byte(compVariableOrExpression)
comp shortInt explicit shortInt(compVariableOrExpression)
comp real explicit real(compVariableOrExpression)

caveats

  • Explicit typecasts disable range checks altogether in the complete line of code.

see also