Wiki Home

VFP Function BINTOC


Namespace: WIN_COM_API
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)
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

function BinToChar
  parameters tnInput, tnWidth
  private tcOutput
  tcOutput = ""
  do while(m.tnInput>0)
     tcOutput = m.tcOutput + chr( m.tnInput%256)
     tnInput = int(m.tnInput/256)
  enddo
  return padr(m.tcOutput,m.tnWidth,chr(0))


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 )