/******************************************************************************

	MakDBf()

	This function converts a binary number to it's ASCII equivalent.
The resultant ASCII string is left in the DBf buffer.  The second argument
to this routine specifies the radix to be used.  If the radix is decimal,
then a minus sign may be appended to the number.  If the radix is not
decimal,  then no minus sign is appended.

*****************************************************************************/

#include "ZPort.h"		/* define portability identifiers */
#include "DefTeco.h"		/* define general identifiers */

extern	char	ZChrIt();	/* type convert LONG to char */

EXTERN	char	*DBfBeg;	/* digit buffer beginning */
EXTERN	char	*DBfPtr;	/* digit buffer pointer */


VOID MakDBf(Binary, NRadix)	/* make digit buffer */
LONG Binary;			/* binary number to be converted */
DEFAULT NRadix;			/* radix to be used: 8, 10 or 16 */
{
	LOCAL	char	c;
	LOCAL	char	*i;
	LOCAL	char	*j;
	LOCAL	int	k;
	LOCAL	BOOLEAN	LZero;		/* leading zero flag */
	LOCAL	BOOLEAN	Negtiv;		/* negative or positive flag */
	LOCAL	unsigned LONG TmpBin;	/* temporary binary value */


	DBfPtr = DBfBeg;			/* init digit buffer ptr */
	switch (NRadix) {
	    case 10:
		if (Negtiv = (Binary < 0L))	/* get sign, and if negative */
			Binary = -Binary;	/* make Binary positive */
		do				/* for each digit */
			*DBfPtr++ = ZChrIt(Binary % NRadix) + '0';
		while ((Binary /= NRadix) > 0);
		if (Negtiv)			/* if it was negative */
			*DBfPtr++ = '-';	/* add the minus sign */
		for (i=DBfBeg, j=DBfPtr-1L; i<j; i++,j--)  /* reverse it */
			{
			c = *i;
			*i = *j;
			*j = c;
			}
		break;
	    case 8:
		if (Binary == 0)
			*DBfPtr++ = '0';
		else
			{
			LZero = YES;
			for (k= -1;k<=29;k+=3)
				{
				TmpBin = Binary;
				if (k > 0)
					{
					TmpBin = TmpBin << k;
					TmpBin = TmpBin >> 29;
					}
				else
					TmpBin = TmpBin >> 30;
				*DBfPtr = ZChrIt(TmpBin) + '0';
				if (LZero)
					if (*DBfPtr != '0')
						LZero = NO;
				if (!LZero)
					++DBfPtr;
				}
			}
		break;
	    case 16:
		if (Binary == 0)
			*DBfPtr++ = '0';
		else
			{
			LZero = YES;
			for (k=0;k<=28;k+=4)
				{
				TmpBin = Binary << k;
				TmpBin = TmpBin >> 28;
				*DBfPtr = ZChrIt(TmpBin) + '0';
				if (*DBfPtr > '9')
					*DBfPtr += '\007';
				if (LZero)
					if (*DBfPtr != '0')
						LZero = NO;
				if (!LZero)
					++DBfPtr;
				}
			}
		}
}
