Type conversions

If data types are passed to functions, or if arguments of different data types are used in expressions, then CTRL always tries to convert the different types to a common data type, or to the expected type for arguments.

This can lead to unexpected results !

Explicit type conversions

The cast operator is used for an explicit type conversion. The cast operator is specified directly before the expression, whose type should be converted, which means that the cast operation is performed before the expression is applied.

The value of the expression is converted into the type specified by the cast operator.

string s1 = "123";
float f1 = 12.345;
// Result data point is not obvious by reading the code
//s1 could be converted into a float or f1 into a string
s1 + f1
// F1 is converted into a string and added to s1
s1 + (string)f1;

It is now also possible to convert a blob to a dyn_char and back again (e.g. in assignment, cast or function call arguments).

Conversion to string values

If a value of a simple data type (i.e. not a dynamic field) is assigned to a string variable, then sprintf() with minimum format specifications is automatically applied to the value in question. The string variable then contains the result of this operation. When converting a bit32 type, a string is returned consisting of the corresponding '0'-'1' combinations.

Example

string bucket;
char contents = 'a';
bucket = 3.14;
// bucket has the value "3.14" , which is a set of characters not digits !
bucket = contents;
// bucket has the value "a"

The conversion of a DpIdentifier to a string with unknown DP returns the number of the DpIdentfier.

Conversion from string values

Strings can easily be converted to int, unsigned or float.

With float one must consider the country setting for the output of numerical values. The decimal point character may change depending on the country.

If a string value is assigned to a 32bit variable, then every "1" in the string sets the corresponding bit in the 32bit variable, and every "0" clears the corresponding bit. Other characters in the string cause no change in the respective bit.

string s = "00000011111100000011111100000011";
bit32 b = s;

The character string then writes this value to the bit32 variable.

Strings containing hexadecimal notation for bytes can be assigned to a blob variable.

Example

string s = "FFAB003A";
blob b = s;

The blob now contains 4 bytes with the Hex values FF, AB, 00 and 3A.

The string can also take this form

string s = "FF,AB,00,3A";

Conversion of time values

Time values are handled in a particular way. Such variables can themselves be converted into character strings via parsing, while strings can also be converted directly into time values. The format in which time values are passed is: (2-digit year figures are also allowed)

[[YY]YY.MM.DD] HH[:MM[:SS[.mmm]]]

The hour section of the time of day (0-24 hour) must thus be specified as a minimum. The optional date section is separated from the hours by a space. If no date is passed, then 1. 1. 1970 is taken as the default value for the date.

If two different data types are used together, then both arguments must be cast to the same data type. Which of two different data types is chosen depends on the defined casting order. Each data type has an order number. string=11, time=12. See chapter string for the description of the data type string. Conversion is done to the larger type with the higher ordinal number. string + time means: conversion from string to time, then addition of the correct expression (time). The result is time.
CAUTION: This is a common mistake: DebugTN("Time: " + someTimeVar); // wrong

The right code is: DebugTN("Time: " + (string)someTimeVar); // correct

Example

time zerohoursten;
string tstring;
zerohoursten = "00:10";
/* The time zerohoursten is set using the string "00:10". */
tstring = zerohoursten;
DebugN(tstring);
/* tstring contains the value "1970.01.01 00:10:00.000" */

If a string with a two-figure year is assigned to a time, then a year < 70 means a year in the 21st century (20xx), and a year >= 70 means a year 19xx (20th c.).

Example

This example shows how time variables are casted to string format.

main()
{
  time timeEnd=getCurrentTime();
  time timeStart= timeEnd-120;
  /* Time must be casted to a string */
  string s="Starttime: "+(string)timeStart+" Endtime: "+(string)timeEnd;
  DebugN(s);
}

Example

This example shows how string variables can be directly used as time variables.

main()
{
  string StrTimVal = "1978.12.15 15:00:00:000";
  time timeVal=StrTimVal;
  DebugN(timeVal);
}

Example

This example shows how time variables are cast (converted) to string format.

main()
{
  time timeEnd=getCurrentTime();
  time timeStart= timeEnd-120;
  /* A time variable must be casted to a string if you want to use a time variable as a string */
  string s="Starttime: "+(string)timeStart+" Endtime: "+(string)timeEnd;
  DebugN(s);
}

Example

 main()
{ 
  time t1, t2; t1 = "98.1.1";
  // outputs 1.1.1998
  t2 = "29.10.11;
  // outputs 11.10.2029
  DebugN(t1, t2);
}

If the string "now" is passed then it is the current time.

Conversion of int, unsigned and float values into time values

int, unsigned and float values can also be directly assigned to time values: int and unsigned values, plus the integer digits of floats are taken as seconds, while the decimal places in float values are treated as milliseconds.

Example

time t1, t2;
t1 = "2000.01.01 12:00:00";
//...
t1 -= 3600;
// The value of t1 is decreased by one hour
t2 += 1.003;
// The value of t2 is increased by 1003 milliseconds