Difference between revisions of "Typecasting"

From Flowcode Help
Jump to navigationJump to search
 
Line 1: Line 1:
==Operators==
+
==Introduction==
  
Typecasting is a way of specifying what we want in terms of the assignment and the values passed to the assignment.
+
Variables can have different 'types', such as Byte, Long and String. Converting a variable from one type to another is often necessary within a program. Typecasting occurs when a particular variable type is converted to another type and has 2 forms:
  
 +
* Implicit conversion - when the type of a calculation result or assignment is changed automatically
 +
* Explicit conversion - when the user manually specifies that a variable or result should be converted to a different type
  
Typecast operators:
+
 
 +
==Implicit conversion between integer types==
 +
 
 +
Implicit type conversion typically occurs when an integer variable is assigned to the result of a calculation, and sometimes this can produce unexpected results. Especially when the type of the variable is not appropriate for the calculation result.
 +
 
 +
For example, consider when a Byte variable is assigned to the result of 3 * 100. The result of the calculation is 300, but this is too big for a Byte variable (which can only hold values between 0 and 255) and the result will actually be 44.
 +
 
 +
A similar thing happens when an 'unsigned' variable (e.g. UInt) is set to a negative value. The variable type is inappropriate and is not designed to contain a negative value, and so the value is converted to be a positive value. A UInt variable set to the value -1 will actually show 65535 as its value.
 +
 
 +
The same can be seen when a division occurs and the result would not be an integer. For example, if an integer is set to 20 / 3, the result showing would be 6 (even though the actual result would be 6.66666666...).
 +
 
 +
For these reasons, it is very important to set the variable type so it is appropriate for the values it is designed to hold.
 +
 
 +
 
 +
==Explicit Typecasting Operators==
 +
 
 +
Explicit typecasting is a way of specifying what we want in terms of the assignment and the values passed to the assignment.
 +
 
 +
 
 +
Explicit typecast operators are:
  
 
*string
 
*string

Latest revision as of 15:49, 30 September 2024

Introduction

Variables can have different 'types', such as Byte, Long and String. Converting a variable from one type to another is often necessary within a program. Typecasting occurs when a particular variable type is converted to another type and has 2 forms:

  • Implicit conversion - when the type of a calculation result or assignment is changed automatically
  • Explicit conversion - when the user manually specifies that a variable or result should be converted to a different type


Implicit conversion between integer types

Implicit type conversion typically occurs when an integer variable is assigned to the result of a calculation, and sometimes this can produce unexpected results. Especially when the type of the variable is not appropriate for the calculation result.

For example, consider when a Byte variable is assigned to the result of 3 * 100. The result of the calculation is 300, but this is too big for a Byte variable (which can only hold values between 0 and 255) and the result will actually be 44.

A similar thing happens when an 'unsigned' variable (e.g. UInt) is set to a negative value. The variable type is inappropriate and is not designed to contain a negative value, and so the value is converted to be a positive value. A UInt variable set to the value -1 will actually show 65535 as its value.

The same can be seen when a division occurs and the result would not be an integer. For example, if an integer is set to 20 / 3, the result showing would be 6 (even though the actual result would be 6.66666666...).

For these reasons, it is very important to set the variable type so it is appropriate for the values it is designed to hold.


Explicit Typecasting Operators

Explicit typecasting is a way of specifying what we want in terms of the assignment and the values passed to the assignment.


Explicit typecast operators are:

  • string
  • float
  • signed
  • unsigned


Converting to string

If we want to assign a numeric variable as an ASCII string then we can use the "STRING" typecast operator.


StringVar = STRING NumericVar


This can be taken further and used on results of calculations etc.


StringVar = STRING(NumericVar * 5)


Converting to float

If we want to assign a integer variable as a floating point variable then we can use the "FLOAT" typecast operator.


FloatVar = FLOAT IntVar


This can be taken further and used on results of calculations etc.


FloatVar = FLOAT (IntVar * 5)


To assign a float status to a fixed value we can do one of the following.


  • FloatVar = FLOAT 5
  • FloatVar = 5.0