The bintoc() and ctobin() functions convert between integers and a string representation of the number. Important - that is *A* string representation not *THE* representation (as if there were only one).
You might expect BINTOC to simply 'map the bytes' from an internal integer representation to the corresponding bytes as part of a string, but it does not. It has been deliberately written to maintain sorting order for positive and negative numbers. Thus it is a BIG-endian format, and the sign bit is inverted from normal two's complement.
Examples (using 16-bit numbers)
- bintoc( 4, 2 ) = chr( 0x80 ) + chr(0x04 )
- bintoc ( -4, 2 ) = chr( 0x7F ) + chr( 0xFC )
If you have string representing binary values, then it will only be compatible with bintoc/ctobin if it is also big-endian with inverted sign bit.
Typical pre-VFP code might be
parameters tnInput, tnWidth
tcOutput = ""
tcOutput = m.tcOutput + chr( m.tnInput%256)
tnInput = int(m.tnInput/256)
This function produces a LITTLE-ENDIAN string and does not invert the sign bit. (It also doesn't handle negative inputs, but no one's perfect)
Category Code Samples Category UDF
( Topic last updated: 2002.08.22 12:55:41 AM )