Converting Numeric Data Types

When you work with numbers there will come a time when you will need to convert a variable from one data type to another data type. In FreeBasic there are two types of data conversion. Implicit, where the compiler will automatically convert the data types during an assignment or calculation, and explicit using one of the predefined conversion functions.

Implicit Data Conversion

Implicit data conversion occurs either through an assignment statement, or as the result of a calculation. Where implicit data conversion can cause a problem is in the loss of precision that can result when converting a large data type to a smaller data type. While implicit data conversion eases the programming process somewhat, you want to make sure that the results you are getting are what you expect. You should always check the result of an implicit conversion to make sure the range of values being converted is in the expected range.

Explicit Data Conversion

There is an alternative to implicit data conversion, explicit data conversion where you use one of FreeBasic’s built-in conversion functions. Since these functions are designed for conversion, they return consistent results and are unlikely to change even if the implicit rules of the compiler change. Even though it is more work, it is always safer to explicitly convert data to the needed data type before carrying out operations on that data.

Numeric Data Conversion Functions

The following table lists all the conversion functions. Keep in mind that these functions do not check for overflow, so be sure that the value passed to these functions is in the expected range.

Function Syntax Purpose Comment
Cast B = Cast(datatype, expression) Convert expression to data-type listed. Datatype is any of the available FreeBasic data-types. Expression is a numeric value, variable or expression.
CByte B = CByte(expression) Convert expression to byte. Expression must be in the range –128 to 127.
CDbl B = CDbl(expression) Convert expression to double. Expression must be in the range of -2.2E-308 to +1.7E+308.
Cint B = Cint(expression) Convert expression to integer. Expression must be in the range of –2,147,483,648 to 2,147,483,647.
CLng B = CLng(expression) Convert expression to long. Long is an alias for integer. Expression must be in the range of –2,147,483,648 to 2,147,483,647.
ClngInt B = ClngInt(expression) Convert expression to long int. Expression must be in the range of –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
CShort B = CShort(expression) Convert expression to short. Expression must be in the range of -32768 to 32767.
CUByte B = CUByte(expression) Convert expression to unsigned byte. Expression must be in the range of 0 to 255.
CUInt B = CUInt(expression) Convert expression to unsigned integer. Expression must be in the range of 0 to 4,294,967,296.
CULngInt B = CULngInt(expression) Convert expression to long unsigned integer. Expression must be in the range of 0 to 18,446,744,073,709,551,615.
CUShort B = CUShort(expression) Convert expression to unsigned short. Expression must be in the range of 0 to 65535.
CSng B = SCng(expression) Convert expression to single. Expression must be in the range of 1.1 E-38 to 3.43 E+38.

Rounding

When examining conversion functions it is important to understand how the function rounds with both negative and positive numbers. The FreeBasic conversion functions round numbers up, making positive number more positive and negative numbers more negative.

You can use both numbers and expressions in the conversion functions. Keep in mind that if you pass an expression to a conversion function, the evaluated expression must not exceed the limit of the target data type, or an overflow will occur.

Using Conversion Functions in Macros

One area where the conversion functions listed in the previous table are useful is in the creation of macros. A macro is a small piece of executable code that the compiler inserts into the program at designated areas. Macros are defined using the #Define compiler directive. #Define has the format #Define name symbol. When the compiler encounters name within the program code, it is replaced with symbol. Here is an example of a macro that creates a DWord data type.

'Macro created by v1ctor
#define MAKDWORD(x,y) (cint(x) shl 16 or cint(y))

The macro that packs two numbers into a single uinteger. One number is packed into the hi-word of the uninteger and one number is packed into the lo-word of the uinteger. The conversion function Cint is used since you will not always know the data type being passed to the macro, and you want to be sure that you are working with known quantities.

Decimal Rounding Functions

FreeBasic has two built-in rounding functions for use with decimal numbers. The following table lists the functions along with their explanations.

Function Syntax Comment
Int B = Int(expression) Returns an integer value less than or equal to the input.
Fix B = Fix(expression) Returns the integer part of the input.

As with the other conversion functions, these should be tested with both negative and positive numbers to see how the functions behaves with a range of values.