Logo

Hacker’s Guide to Visual FoxPro
An irreverent look at how Visual FoxPro really works. Tells you the inside scoop on every command, function, property, event and method of Visual FoxPro.

BINTOC(), CTOBIN()

These two functions let you change between integers and a character representation of their values. Their raison d’etre is to allow more efficient indexing.

Usage

cCharValue = BinToC( nInteger [, nChars] )
nInteger = CToBin( cCharValue )

Parameter

Value

Meaning

nInteger

Numeric

The number to be converted to character or the result of the conversion.

cCharValue

Character

The character-string representation of nInteger.

nChars

1

Use one character to represent nInteger.

2

Use two characters to represent nInteger.

4 or omitted

Use four characters to represent nInteger.

The character set we use has 256 characters. Internally, they’re represented by the numbers from 0 to 255. The number system we use has only 10 characters, the digits from 0 to 9. If we use characters to represent numbers (as opposed to the other way around, which is how we usually think of things), we can represent large numbers using fewer characters than digits. That is, a single character can represent up to 256 different numbers, while a one-digit number can have only 10 different values. In essence, representing numbers using the character set lets us work in base 256.

These functions convert between numbers and their character representations—that is, converting to and from base 256. We can choose to use one, two or four characters to represent a single number. However, the range of values is restricted based on the number of characters used. This makes sense, since one character has only 256 distinct values, while two characters give us 256^2 and four offer 256^4.

To confuse matters, though, instead of mapping directly (number 0 to CHR(0), number 1 to CHR(1), and so forth), these functions can work with both positive and negative values. So a string of CHR(0)’s is used for the smallest number in the range, and the largest number in the range is converted to a string of CHR(255)’s. With one character, this means that CHR(0) represents -128 and CHR(255) represents 127. This means, among other things, that using these functions for converting between hex and decimal is, at best, complicated. Why would we want to do this? For space efficiency. It takes a lot less space to store large numbers in base 256 than in base 10 (used for most numeric types). The obvious place to apply this efficiency is in indexing of large tables. Our friend Mac Rubel has done extensive testing in this area, and finds that not only do indexes created with BINTOC() take up less storage space than characters, but they’re actually faster to create and to work with. (The big winner in his testing, though, was Integers. For the best overall performance, go with Integer keys.)

Example

? BINTOC( -128, 1)  && returns CHR(0)
? BINTOC(127,1)      && returns CHR(255)
? CTOBIN("zzzz")     && returns -92636550
* Suppose you have an integer primary key field, iPKey
INDEX ON BINTOC(iPKey) TAG iPKey

See Also

Asc(), Chr(), Transform()