Function L2bin() Foundation

Converts a numeric value to 32 bit integer.

Syntax
L2bin( <nInteger> ) --> cBinary
Parameters
<nInteger>
<nInteger> is a numeric expression whose value is converted into a binary format. If the value is not an integer, the decimal places are truncated.
Return

The return value of L2bin() is a character string with four characters containing the numeric value as a 32 bit integer.

Description

L2bin() converts numeric values in the range of -2147483648 to +2147483647 to binary representation. The function returns a character string with four characters. L2bin() ignores decimal places and only converts integers. The counterpart of L2bin() is Bin2l().

L2Bin() is frequently used in connection with the low level file functions. This is sometimes done to allow definition of special or unique file formats or to provide data compression for integer values.

Examples
L2bin()
// In the example the previous value is recorded before 
// a change to a DBF file. The data record number is 
// converted with L2bin() to the binary format. 

#include "Fileio.ch" 

PROCEDURE Main 
   MEMVAR  PartNo 
   PRIVATE PartNo := Space(10) 
   USE Parts 

   @ 10,10 SAY "Part No:" GET memvar->PartNo 
   READ 

   IF Updated() 
      LogChange( "PARTS", "PARTNO" ) 
      REPLACE parts->PartNo WITH  memvar->PartNo 
   ENDIF 

RETURN 

FUNCTION LogChange( cAlias, cFieldName ) 
   LOCAL nArea   := Select(cAlias) 
   LOCAL nField  := (nArea)->(FieldPos(cFieldName)) 
   LOCAL xValue  := (nArea)->(FieldGet(nField)) 
   LOCAL cType   := Valtype( xValue ) 
   LOCAL nHandle := FOpen( "CHANGES.LOG", FO_READWRITE ) 

   DO CASE 
   CASE cType == "N" ; xValue := LTrim(Str(xValue)) 
   CASE cType == "D" ; xValue := DtoC(xValue) 
   CASE cType == "L" ; xValue := IIf(xValue,"T","F") 
   ENDCASE 

   FSeek(  nHandle, 0, FS_END ) 
   FWrite( nHandle, L2bin( (nArea)->(RecNo()) ) ) 
   FWrite( nHandle, cAlias+Chr(0)+I2bin(nField) ) 
   FWrite( nHandle, L2bin( Len(xValue) )+xValue ) 

   FClose( nHandle ) 

RETURN FError() 

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.