BINTOC( ) Function

Converts a numeric value to a binary character representation.

BINTOC(nExpression [, eFlags])

Parameters

  • nExpression
    Specifies the value to convert. For eFlags settings 1, 2 or 4, this value is an integer. Otherwise, the expression data type is based on the eFlags setting.

  • eFlags
    Specifies the length in characters of the returned character string.

    eFlags determines the value you can specify for nExpression. eFlags can either be numeric or character. The following table lists the permissible values for eFlags and the corresponding range of values for nExpression:

    eFlags

    nExpression Range

    1

    –128 to 127

    2

    –32,768 to 32,767

    4

    –2,147,483,648 to 2,147,483,647

    This is default setting.

    8

    The range of nExpression depends on its type. Only numeric, float, double, and currency data types are supported with this option. See Visual FoxPro Data and Field Types for the ranges of the numeric, float, double, and currency data types.

    BINTOC( ) returns 8 bytes for this setting.

    F

    nExpression is interpreted as a Float Field Type and BINTOC( ) returns 4 bytes.

    B

    nExpression is interpreted as a Double Field Type and BINTOC( ) returns 8 bytes.

    R

    Reverses the resulting binary expression.

    S

    Prevents the sign bit of number from being toggled (BITXOR).

    If this parameter is omitted, BINTOC( ) returns a character string composed of four characters.

Return Value

Character. BINTOC( ) returns a binary character expression.

Remarks

The eFlags parameter can either be numeric or character. The 'R' and 'S' settings are additive while the others are mutually exclusive. Character settings can be passed in either upper or lower case (e.g., 'R' or 'r'). The following examples show various uses of the eFlags parameter.

? BINTOC(1,1)
? BINTOC(1000,"2")&& same as BINTOC(1000,2)
? BINTOC($12.34,8)
? BINTOC(1, "4RS")&& same as BINTOC(1,"RS")
? BINTOC(-100, "Fr")

You can use BINTOC( ) to reduce the size of indexes for numeric fields containing integer data by passing a numeric eFlags parameter. For example, a numeric field named nPartCode might contain an integer value from 1 to 32,767, which corresponds to a parts classification code. BINTOC( ) lets you convert the value in the numeric field to a smaller character representation. For example, the following command creates an index with a two-character index key:

INDEX ON BINTOC(nPartCode,2) TAG PartCode

When using BINTOC( ) for an 8 byte index creation, you should use 8 and not "B". For 4 byte results, you should use 4 if nExpression is an integer type. Otherwise, you should use 'F' for floating point type.

BINTOC( ) can also be used when working with Win32 API routines where you might need to convert to or from a Win32 struct member. When used for indexing, BINTOC( ) needs to properly handle negative numbers for the resulting binary expression. This is done by using a BITXOR operation on the high bit. This also means that the most significant byte is first. For Win32 API routines on an Intel platform, the architecture follows the little-endian rule where the least significant byte is stored first (memory location with lowest address). The 'R' and 'S' settings allow you to use BINTOC( ) to work more efficiently with routines that use structs.

See Also

Reference

CTOBIN( ) Function

CREATEBINARY( ) Function

Integer Field Type

Other Resources

Functions

Language Reference (Visual FoxPro)