Data Conversion

Integers, floating point numbers, and strings can represent the same values, but use different methods to store them in memory. Sometimes, simple assignment will instruct the compiler to make the necessary conversion (such as assigning an integer to a floating point variable). However, particularly when using variants, dates, or strings, you may want explicit control.

If you want to treat a memory location as another data type without converting it, then see casting.

See String Functions for additional functions to convert to and from strings.

Visual Basic | Delphi


Visual Basic 6.0

The following functions will convert either a string or an expression to the indicated types. If the expression is outside the result type range, then an error results.
CBool Boolean CByte Byte (0 to 255)
CCur Currency Cdate Date
CDbl Double (Floating point) CDec Decimal (weird)
CInt Integer (16 bits) CLng Long (32-bit Integer)
CSng Single (Floating point) CStr String
Cvar Variant Format String
Int Integer part of number Asc Byte (0 to 255)
Fix Integer part of number Val Numeric
Hex String Str String
Oct String
val, CSng, and CDbl will convert strings to floating point (real) numbers. However, val assumes that the period (.) is the only possible decimal separator, and the other two recognize international decimal and thousands separators, depending on the system's settings.


Delphi

Use ord to convert an enumerated variable to anything else.

Converting real (floating point) numbers to integers (for a complete list, see "Arithmetic routines")


Author: Robert Clemenzi - clemenzi@cpcug.org
URL: http:// cpcug.org / user / clemenzi / technical / Languages / DataConversion.htm