Function Var2Bin() Foundation

Converts any value to a binary character string.

Syntax
Var2Bin( <xValue> ) --> cBinaryString
Parameters
<xValue>
<xValue> is a value of any data type to be converted to a string containing binary data.
Return

The return value is a character string which contains the binary representation of the passed argument.

Description

The function Var2Bin() converts values of all data types to a character string. This includes arrays and code blocks. The return value can be transformed back to the original value when the string is passed to the function Bin2Var(). This allows, for example, the exchange of data between different processes using inter-process communication (IPC). Using IPC, it is possible to send code blocks from work station A to B. As a result, one work station can delegate computing to other work stations (distributed applications).

Conversion of complex data types

If values of data type array, code block or object are passed to Var2Bin() and the returned string is converted back using Bin2Var(), a new reference is created with the same contents as the original value. For example, if two variables containing the same array reference are passed to Var2Bin(), the return values are identical. When the two binary strings are converted back to an array, two different array references are created but the arrays have the same contents.

Conversion of objects

In case of objects, the function Var2Bin() accepts only instances of a class, not the class object itself. It should also be noted that objects may have instance variables whose contents is not persistent (especially XbaseParts).

If the result of the function Var2Bin() is to be stored into a database, the field type of the table must be able to hold binary data. It particularily means that no implicit conversion may take place when reading or writing the data. See Specifications of the DatabaseEngines.

Examples
Convert values to binary strings

// The example shows how to convert values using Var2Bin() and 
// Bin2Var(). The implications of converting arrays are outlined. 

PROCEDURE Main 
   LOCAL xValue1, xValue2 

   xValue1 := xValue2 := Directory() 

   * Variables contain the same array reference 
   ? xValue1 == xValue2                  // result: .T. 

   xValue1 := Var2Bin( xValue1 ) 
   xValue2 := Var2Bin( xValue2 ) 

   * Binary data is identical 
   ? xValue1 == xValue2                  // result: .T. 

   xValue1 := Bin2Var( xValue1 ) 
   xValue2 := Bin2Var( xValue2 ) 

   * Variables contain two different array references 
   ? xValue1 == xValue2                  // result: .F. 

   * The contents of the arrays are identical 
   ? ACompare( xValue1, xValue2 )        // result: .T. 
RETURN 


FUNCTION ACompare( aArray1, aArray2 ) 
   LOCAL nElements := Len(aArray1)       // Number of elements 
   LOCAL lEqual    := (nElements == Len(aArray2)) 
   LOCAL nCount    := 0 
   LOCAL cType 

   DO WHILE lEqual .AND. ++nCount <= nElements 
      cType := Valtype( aArray1[nCount] ) // Get data type 
      lEqual:= (cType == Valtype( aArray2[nCount] )) 

      IF lEqual 
         IF cType == "A"                  // Compare subarrays 
            lEqual := ACompare(aArray1[nCount], aArray2[nCount]) 
         ELSE 
            lEqual := (aArray1[nCount] == aArray2[nCount]) 
         ENDIF 
      ENDIF 
   ENDDO 
RETURN lEqual 
Feedback

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.