Function Char2Var() Foundation

Converts a string into a value of a requested data type.

Char2Var( <cString>, <cValtype> ) --> xValue
<cString> is a character string to be converted. It must be formatted depending on the requested data type <cValtype>:
Formatting rules for Char2Var()
Data type String format
Date String must be formatted like "YYYYMMDD" (see DtoS())
Logic First character must be "T" or "t" for TRUE, and "F" or "f" for FALSE
Numeric Thousands separator is not allowed. Decimal separator must be a dot: "."
<cValtype> can be one of the letters "C", "D", "L", "N" and indicates the data type of the return value. The letter corresponds with the return value of the Valtype() function.

The function returns a value of the requested data type. If the string cannot be converted, the function returns NIL.


The function converts a character string formatted as the string representation of another data type to a value of this data type. It is used in conjunction with Var2Char() which is the inverse function. Note, however, that Char2Var() operates only on the simple data types Character, Date, Numeric and Logic, while Var2Char() converts values of any data type to a string. If the first parameter <cString>does not contain a legal string representation of the requested data type, the function returns NIL.

Converting character strings to other data types
// The example demonstrates return values of Char2Var() 

   ? Char2Var( "20000101", "N" )   // result: 20000101 
   ? Char2Var( "20000101", "D" )   // result: 01/01/00 
   ? Char2Var( "21.4"    , "N" )   // result: 21.4 
   ? Char2Var( "21.4"    , "L" )   // result: NIL 
   ? Char2Var( "T"       , "L" )   // result: .T. 

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.