Functions and Directives

Function Var2Char() Foundation

Converts a variable to it's string representation.

Var2Char( <xValue> ) --> cString
The parameter can have a value of any data type.

The function returns the string representation of the passed value, regardless of locale specific settings.


Var2Char() is used to convert values of any data type, be it simple or complex data types, to a string representation. It ignores any locale specific settings and is the inverse function of Char2Var(). Note, however, that Char2Var() cannot convert the string representation of complex data types (Array, Code block, Object) to the original data type. If this is required, the functions Var2Bin() and Bin2Var() must be used.

Var2Char() converts a code block to a string that is equivalent to PRG code. The string can be converted back to a code block using the macro operator. If you use this approach, you must make sure that the code block does not contain detached LOCAL variables. References to detached LOCALs are lost with Var2Char().

Converting any data type to a string
// The example demonstrates the result of Var2Char() 
// for values of all data types. 

   LOCAL aArray  := { 1, "A" } 
   LOCAL bBlock  := {|x| x + 1 } 
   LOCAL cString := "Hello World" 
   LOCAL dDate   := Date() 
   LOCAL lOk     := .T. 
   LOCAL nNumber := 123.456 
   LOCAL oGet    := Get():new() 
   LOCAL uNil    := NIL 

   ? Var2Char( aArray  )   // result: {1, A} 
   ? Var2Char( bBlock  )   // result: {|x| x + 1 } 
   ? Var2Char( cString )   // result: Hello World 
   ? Var2Char( dDate   )   // result: 20000426 
   ? Var2Char( lOk     )   // result: T 
   ? Var2Char( nNumber )   // result: 123.456 
   ? Var2Char( oGet    )   // result: Get 
   ? Var2Char( uNil    )   // result: NIL 

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.