|
Type Conversion |
Top Previous Next |
Variables can derive their value from other variables; in other words, you can assign a variable to another variable. A simple example of this would be:
dim x, y as byte x = 5 ' x is now 5 y = x ' y is now 5 as well
|
However, as covered above, there are several types of variables, and not all of them can handle the same data. For example, what would happen if you assigned a variable of type byte the value intended for a variable of type word?
Table below details all possible conversion situations.
|
C o n v e r t i n t o |
||||||||
C o n v e r t
f r o m |
|
Byte |
Word |
Dword |
Char |
Short |
Long |
Real |
String |
Byte |
--- |
OK |
OK |
Reinterpret |
OK |
OK |
OK |
OK |
|
Word |
Truncate |
--- |
OK |
Reinterpret Truncate |
Reinterpret |
OK |
OK |
OK |
|
Dword |
Truncate |
Truncate |
--- |
Truncate |
Truncate |
Reinterpret |
OK |
OK |
|
Char |
Reinterpret |
Reinterpret |
Reinterpret |
--- |
OK |
OK |
OK |
OK |
|
Short |
Reinterpret Truncate |
Reinterpret |
Reinterpret |
Truncate |
--- |
OK |
OK |
OK |
|
Long |
Reinterpret Truncate |
Reinterpret |
Truncate |
Truncate |
Truncate |
--- |
OK |
OK |
|
Real |
Fraction ??? |
Fraction ??? |
Fraction ??? |
Fraction ??? |
Fraction ??? |
Fraction ???? |
--- |
OK fstr* |
|
String |
OK |
OK |
OK |
OK |
OK |
OK |
OK |
--- |
|
*fstr is a functional equivalent of ftostr, but without mode and rnd parameters.
Conversions without loss
Conversions marked as "OK" incur no loss -- the value being passed from variable of one type to variable of another type remains unchanged. For example, conversion from word into dword is done without any loss, because 32-bit word variable can hold any value that the 16-bit word variable can hold.
Conversions that cause reinterpretation
Conversions marked with "Reinterpret" mean that although binary data held by the receiving variable may be the same this binary variable may be interpreted differently on the destination "side". As an example, see this conversion from byte into char:
dim x as byte dim c as char x = 254 c = x ' c now contains the binary value of 254, which is interpreted as -2
|
In the above example, both x and c will contain the same binary data. However, c is a signed 8-bit value, so binary contents of 254 mean -2. Strictly speaking, this reinterpretation will only happen if the value of x exceeds maximum positive number that c can hold -- 127. If x<=127 conversion will not cause reinterpretation. For example, if x=15 then doing c=x will result in c=15 as well.
In fact, in some cases, conversion from unsigned type to a signed time will never result in the reinterpretation. This is when the maximum value that the source unsigned variable can hold can always fit in the range of positive values that the signed destination variable can hold. Example: conversion from byte (value range 0-255) to short (value range -32768 to 32767) will never result in the reinterpretation.
Conversion from signed type into unsigned type will always cause reinterpretation if the source variable contained a negative value.
Conversions that cause truncation
Conversions marked with "Truncate" mean that part of the binary data (on the most significant side) may be lost during the conversion. For example, converting from word type into byte type will only leave 8 bits of the original 16-bit value:
dim x as byte dim w as word w = 12345 'hex representation of 12345 is 3039 x = w ' now x contains 57. Why? Because only '39' of '3039' could fit in, and decimal of &h39 is 57.
|
Notice, that some conversions will cause reinterpretation and truncation at the same time!
Conversions that round the number (remove fractions)
Conversions from real type into any other numerical type will cut off the fraction, as real is the only type that can hold fractions. Such conversions are marked as "Fraction" in the table above.
Conversions that implicitly invoke functions available to the user
Some conversions automatically invoke functions (syscalls) available for use in your program. In such cases the table above lists the name of the function invoked. For example, conversion from byte into string relies on the str function. Two ways of conversion below produce identical result:
dim x as byte dim s as string s = str(x) ' explicit invocation s = x ' implicit invocation. Compiler is smart enough to use str for this conversion
|
Conversion of Boolean Variables
Boolean variables are actually stored as byte type variables; thus, all notes above for byte type variables hold true for boolean variables as well.
Conversion of Enumeration Types
User-defined enumeration types are held in various variable types, depending on the values associated with the constants within the enumeration type. This is described in detail under User-Defined Types. Thus, they are converted according to the variable type used to store them (described above).