#include "PalmType.h"

void p_ltoa(ULONG iVal, PXCHAR pBuf, UCHAR iRadix)
{
    PXCHAR_D p;            // pointer to traverse string 
    PXCHAR_D pFirstDigit;  // pointer to first digit 
	DCHAR iTmp;          // temp UCHAR 

    p = pBuf;
    pFirstDigit = p;        // save pointer to first digit 

    do 
    {
        iTmp = (UCHAR)(iVal % iRadix);
        iVal /= iRadix;       // get next digit 

        // convert to ascii and store 
        if (iTmp > 9)
		{
            *p++ = iTmp - 10 + 'a';  // a letter 
		}
        else
		{
            *p++ = iTmp + '0';       // a digit 
		}
    } while (iVal > 0);

	// We now have the digit of the number in the buffer, but in reverse order.  Thus we reverse them now. 
	
    *p-- = '\0';            // terminate string; p points to last digit 

    do 
    {
		iTmp = *p;
        *p = *pFirstDigit;
        *pFirstDigit = iTmp;   // swap *p and *pFirstDigit 
        --p;
        ++pFirstDigit;         // advance to next two digits 
    } while (pFirstDigit < p); // repeat until halfway 
}

